Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52076 > unrolled thread

HTTP post with urllib2

Started bycerr <ron.eggler@gmail.com>
First post2013-08-06 15:52 -0700
Last post2013-08-07 01:28 +0100
Articles 7 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  HTTP post with urllib2 cerr <ron.eggler@gmail.com> - 2013-08-06 15:52 -0700
    Re: HTTP post with urllib2 Joel Goldstick <joel.goldstick@gmail.com> - 2013-08-06 19:08 -0400
      Re: HTTP post with urllib2 cerr <ron.eggler@gmail.com> - 2013-08-06 16:35 -0700
        Re: HTTP post with urllib2 Joel Goldstick <joel.goldstick@gmail.com> - 2013-08-06 19:48 -0400
    Re: HTTP post with urllib2 MRAB <python@mrabarnett.plus.com> - 2013-08-07 01:14 +0100
      Re: HTTP post with urllib2 cerr <ron.eggler@gmail.com> - 2013-08-07 09:20 -0700
    Re: HTTP post with urllib2 Chris Angelico <rosuav@gmail.com> - 2013-08-07 01:28 +0100

#52076 — HTTP post with urllib2

Fromcerr <ron.eggler@gmail.com>
Date2013-08-06 15:52 -0700
SubjectHTTP post with urllib2
Message-ID<2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com>
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!

[toc] | [next] | [standalone]


#52080

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-08-06 19:08 -0400
Message-ID<mailman.280.1375830517.1251.python-list@python.org>
In reply to#52076
On Tue, Aug 6, 2013 at 6:52 PM, cerr <ron.eggler@gmail.com> wrote:
> 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!
> --
> http://mail.python.org/mailman/listinfo/python-list

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



-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#52084

Fromcerr <ron.eggler@gmail.com>
Date2013-08-06 16:35 -0700
Message-ID<59b6f7db-a1e6-4326-8c87-49f2678b7db5@googlegroups.com>
In reply to#52080
On Tuesday, August 6, 2013 4:08:34 PM UTC-7, Joel Goldstick wrote:
> On Tue, Aug 6, 2013 at 6:52 PM, cerr <ron.eggler@gmail.com> wrote:
> 
> > 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'

I guess so, I got this from an example.... copy & paste :x

> 
> > response = urllib2.urlopen(req, 120)
> 
> 
> 
> the docs say req should be a url.  Is it?

no, it's an instance of req = urllib2.Request()
> 
> > 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!
> 
> > --
> 
> > http://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> 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...

[toc] | [prev] | [next] | [standalone]


#52090

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-08-06 19:48 -0400
Message-ID<mailman.287.1375832947.1251.python-list@python.org>
In reply to#52084
On Tue, Aug 6, 2013 at 7:35 PM, cerr <ron.eggler@gmail.com> wrote:
> On Tuesday, August 6, 2013 4:08:34 PM UTC-7, Joel Goldstick wrote:
>> On Tue, Aug 6, 2013 at 6:52 PM, cerr <ron.eggler@gmail.com> wrote:
>>
>> > 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'
>
> I guess so, I got this from an example.... copy & paste :x

That's not a very good answer!  Honest, but really.  Just because you
can cut and paste, doesn't mean you are learning to program.
>
>>
>> > response = urllib2.urlopen(req, 120)
>>
>>
>>
>> the docs say req should be a url.  Is it?
>
> no, it's an instance of req = urllib2.Request()
>>
>> > 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!
>>
>> > --
>>
>> > http://mail.python.org/mailman/listinfo/python-list
>>
>>
>>
>> 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...

I'm sure there is.  I'm not a pro at urllib, not even requests but
when I have used it, it made a lot more sense.

It got changed in python 3, so the python core group seemed not to
like how the earlier modules worked.

At any rate.  wait a while.  Someone on the list will give you more
specific advice I'm sure
> --
> http://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#52092

FromMRAB <python@mrabarnett.plus.com>
Date2013-08-07 01:14 +0100
Message-ID<mailman.288.1375834490.1251.python-list@python.org>
In reply to#52076
On 06/08/2013 23:52, cerr wrote:
> 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)""".

[toc] | [prev] | [next] | [standalone]


#52137

Fromcerr <ron.eggler@gmail.com>
Date2013-08-07 09:20 -0700
Message-ID<86b93813-50a2-41b8-b72f-efe20ac57182@googlegroups.com>
In reply to#52092
On Tuesday, August 6, 2013 5:14:48 PM UTC-7, MRAB wrote:
> On 06/08/2013 23:52, cerr wrote:
> 
> > 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)""".

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!

[toc] | [prev] | [next] | [standalone]


#52094

FromChris Angelico <rosuav@gmail.com>
Date2013-08-07 01:28 +0100
Message-ID<mailman.290.1375835312.1251.python-list@python.org>
In reply to#52076
On Tue, Aug 6, 2013 at 11:52 PM, cerr <ron.eggler@gmail.com> wrote:
> ./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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web