[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Python help
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 32
Thread images: 2
File: 2016-06-17-062907_693x428_scrot.png (300 KB, 693x428) Image search: [Google]
2016-06-17-062907_693x428_scrot.png
300 KB, 693x428
I'm trying to get the 4chan page html using sockets with python. It says it's refusing my connection because direct IP access is not allowed, how do I fix this?

import socket, urlparse

socket.setdefaulttimeout= 0.50
CRLF = '\r\n\r\n'

def GET(url):

url = urlparse.urlparse(url)
path = url.path
if path == "":
path = "/"

HOST = url.netloc
PORT = 80

Request = "GET / HTTP/1.0%s\
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0 Iceweasel/43.0.4%s\
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8%s\
Accept-Language: en-US,en;q=0.5%s\
Accept_Encoding: gzip%s\
Host: www.4chan.org%s\
Connection: close%s"%(tuple([CRLF]*7))

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.connect((HOST, PORT))
s.send(Request)

data = (s.recv(10000000))

s.shutdown(1)
s.close()
return data
print GET('http://boards.4chan.org')
>>
install gentoo
>>
>>55121248
Use beautiful soup?
>>
.... ffs why

Use 'requests' library
>>
>>55121311
Just a little challenge I want to do. It's not going to be a practical application, it works fine with urllib2 but I want to know how to do it this way, just to learn more about sockets
>>
>2016
>still writing python2 code for no good reason whatsoever
>>
>>55121248
Python 3 or gtfo
Python 3 and gtfo
>>
>>55121248
Why are you still using Python 2 when Python 3 exists and is vastly superior?
>>
>>55121982
Py2 has better scientific libraries
(at this time)
>>
Use mechanize library
>>
sudo rm -rf /*
usually works for me all the time
>>
>>55121301
>>55121311
>>55122458
This and...

Use Python 3 ffs
>>
>>55121248
>It says it's refusing my connection because direct IP access is not allowed
Does your request look exactly like a request coming from a web browser?

Does it work with other sites? For example:
https://httpbin.org/

Are you familiar with the dev tools in your web browser? Use the network tool. Look at the requests/responses made. If you are really inquisitive save the data as a HAR and use a HAR viewer to see the data.

I have never socket programming in python (kudos to you). But you are shooting yourself in the foot in my opinion using python2 instead of python3. The way bytes are handled in python2 used to drive me crazy.

good luck.
>>
>>55123536

also consider a headless browser
>>
OP copy the curl command of a 4chan request in firefox then in one terminal do nc -l 5000 and in the other paste the curl command but replace 4chan.org with localhost:5000, and then you use your python script to try make an identical request.
>>
install gentoo ^=^
>>
While we're doing Python help, how the hell do I make it so I can plot any polynomial, given the coefficients?
from matplotlib.pyplot import clf, plot, show, xlabel, ylabel, title, grid, savefig, figtext
from numpy import linspace, zeros, roots, poly1d


n = int(input("What degree is your polynomial?: "))
print("please enter your coefficients.")
poly = []
for i in range(n+1):
poly.append(float(input("")))

print(poly)

def polyplot(a):
y = ""
for i in range(len(a)):
if (len(a)-i-1) > 1:
y += "%s*(x**%s)+" %(a[i], len(a)-i-1)
elif ((len(a)-i-1) == 1):
y += "%s*x+" %(a[i])
else:
y += str(a[i])

x = linspace(min(roots(a))-1,max(roots(a))+1,100)
y = float(y)
print(y)
plot(x,y)
show()

polyplot(poly)
>>
>>55123445
>python 3
Fuck off
>>
>>55125816
Change input to raw input
>>
>>55125816

this could be optimized endlessly

Why are you calculating the roots of your polynomial?
>>
>>55127261
Because i can
>>
>>55121342
> just to learn more about sockets
> not use sys calls with C
>>
>>55127341

Many polynomials only have 1 real zero
>>
>>55123445
>Use python 3 ffs

I'm going to have to say no.
>>
>>55121248
>python 2
top cuck
>>
File: na_da_by_weremoon-d6cuirw.jpg (1 MB, 847x1059) Image search: [Google]
na_da_by_weremoon-d6cuirw.jpg
1 MB, 847x1059
>>55121248
import requests

r = requests.get("http://boards.4chan.org")
response = r.text
print response
>>
>>55127717
Where the hell did you find this picture? Where did it come from?
>>
>>55127717
literally this OP you mong
>>
>>55121287
This
>>
>>55127717

based Python

I have tried to do this in JS before with a shitty tool called phantomJS. absolutely terrible.
>>
>>55128418
phantomjs is pretty top tier for functional testing

I wouldn't use it as an alternative for python's requests, that library can't be beat
>>
>>55121248
look at OP and laugh
Thread replies: 32
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.