HTTP post with urllib2

C

cerr

Hi,

Why does this code:

#!/usr/bin/python


import urllib2
from binascii import hexlify, unhexlify

host = "localhost"
uri="/test.php"
data ="\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\x64" #Hello World
url="http://{0}{1}?f=test".format(host, uri)
req = urllib2.Request(url, data,{'Content-Type': 'application/octet-stream'})
req.get_method = lambda: 'PUT'
response = urllib2.urlopen(req, 120)
retval = response.read()
print "RETVAL "+retval



return me this:

../post.py
Traceback (most recent call last):
File "./post.py", line 13, in <module>
response = urllib2.urlopen(req, 120)
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 398, in open
req = meth(req)
File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_
'Content-length', '%d' % len(data))


I don't get it, what's going on here?

Thank you!
 
J

Joel Goldstick

Hi,

Why does this code:

#!/usr/bin/python


import urllib2
from binascii import hexlify, unhexlify

host = "localhost"
uri="/test.php"
data ="\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\x64" #Hello World
url="http://{0}{1}?f=test".format(host, uri)
req = urllib2.Request(url, data,{'Content-Type': 'application/octet-stream'})
req.get_method = lambda: 'PUT'

What does the above line do? is it the same as req.get_method = 'PUT'
response = urllib2.urlopen(req, 120)

the docs say req should be a url. Is it?
retval = response.read()
print "RETVAL "+retval



return me this:

./post.py
Traceback (most recent call last):
File "./post.py", line 13, in <module>
response = urllib2.urlopen(req, 120)
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 398, in open
req = meth(req)
File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_
'Content-length', '%d' % len(data))


I don't get it, what's going on here?

Thank you!

KInda of ducking your questions, but the requests module is a lot
easier to use and
understand:http://docs.python-requests.org/en/latest/
 
C

cerr

What does the above line do? is it the same as req.get_method = 'PUT'

I guess so, I got this from an example.... copy & paste :x
the docs say req should be a url. Is it?

no, it's an instance of req = urllib2.Request()
KInda of ducking your questions, but the requests module is a lot

easier to use and

understand:http://docs.python-requests.org/en/latest/

But there must be a way to get this working with urllib alone...
 
M

MRAB

Hi,

Why does this code:

#!/usr/bin/python


import urllib2
from binascii import hexlify, unhexlify

host = "localhost"
uri="/test.php"
data ="\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\x64" #Hello World
url="http://{0}{1}?f=test".format(host, uri)
req = urllib2.Request(url, data,{'Content-Type': 'application/octet-stream'})
req.get_method = lambda: 'PUT'
response = urllib2.urlopen(req, 120)
retval = response.read()
print "RETVAL "+retval



return me this:

./post.py
Traceback (most recent call last):
File "./post.py", line 13, in <module>
response = urllib2.urlopen(req, 120)
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 398, in open
req = meth(req)
File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_
'Content-length', '%d' % len(data))


I don't get it, what's going on here?
The docs say """urllib2.urlopen(url[, data][, timeout])""".

You're calling it as """urllib2.urlopen(req, 120)""".

In other words, 'url' is req and 'data' is 120.

It should be """urllib2.urlopen(req, None, 120)""".
 
C

Chris Angelico

./post.py
Traceback (most recent call last):
File "./post.py", line 13, in <module>
response = urllib2.urlopen(req, 120)
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 398, in open
req = meth(req)
File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_
'Content-length', '%d' % len(data))


I don't get it, what's going on here?

You've given a traceback without the actual error. MRAB happened to
figure out the mistake just from what you posted, but in future, do
try to copy a bit more down :)

ChrisA
 
C

cerr

Hi,

Why does this code:




import urllib2
from binascii import hexlify, unhexlify

host = "localhost"

data ="\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\x64" #Hello World
url="http://{0}{1}?f=test".format(host, uri)
req = urllib2.Request(url, data,{'Content-Type': 'application/octet-stream'})
req.get_method = lambda: 'PUT'
response = urllib2.urlopen(req, 120)
retval = response.read()
print "RETVAL "+retval



return me this:


Traceback (most recent call last):
File "./post.py", line 13, in <module>
response = urllib2.urlopen(req, 120)
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 398, in open
req = meth(req)
File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_
'Content-length', '%d' % len(data))


I don't get it, what's going on here?

The docs say """urllib2.urlopen(url[, data][, timeout])""".



You're calling it as """urllib2.urlopen(req, 120)""".



In other words, 'url' is req and 'data' is 120.



It should be """urllib2.urlopen(req, None, 120)""".

Yes, great! That did it! :)
Coming into the office in the morning, sitting down, changing this and get it working! Good way to start my day! :)

Thanks MRAB!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,703
Messages
2,569,328
Members
44,612
Latest member
Billysak

Latest Threads

Top