Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'urllib2': 0.07; 'here?': 0.09; 'req': 0.09; 'seemed': 0.09; 'worked.': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; '6:52': 0.16; '7:35': 0.16; 'lambda:': 0.16; 'subject:post': 0.16; 'timeout)': 0.16; 'url.': 0.16; 'urllib': 0.16; 'wrote:': 0.18; 'module': 0.19; 'import': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'earlier': 0.24; 'cc:2**0': 0.24; 'skip:" 30': 0.26; 'this:': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'host': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'easier': 0.31; 'requests': 0.31; "skip:' 10": 0.31; '13,': 0.31; 'skip:= 40': 0.31; 'file': 0.32; 'open': 0.33; 'url:python': 0.33; '(most': 0.33; 'guess': 0.33; 'skip:# 10': 0.33; 'skip:_ 10': 0.34; 'core': 0.34; 'skip:d 20': 0.34; 'subject:with': 0.35; 'advice': 0.35; 'skip:u 20': 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'data,': 0.36; 'rate.': 0.36; 'url:listinfo': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'list': 0.37; 'thank': 0.38; 'pm,': 0.38; 'recent': 0.39; 'does': 0.39; 'sure': 0.39; 'changed': 0.39; 'url:mail': 0.40; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'is.': 0.60; 'august': 0.61; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'world': 0.66; 'url:f': 0.68; 'pro': 0.69; 'cut': 0.74; 'joel': 0.91; 'url:latest': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=cLMeQYirgFHF1L2u0V989u/E/ypuAs9xxSS65zaAg/k=; b=g6RRiVjCUb8PC78Szdx/AEJwWskNlZSv+rGqY0RCo+r7aHc+6Mi8kyZjW4PxSEpIww 9UOf3ApEG5xwwL4Ykxb6KaxidXDNQXDc9Xk8C1W4scia+ssTGzHDOUFScNTcCdJsUeJe zau8XyblPeVbnwjHUfphvjOms/wKNSf0+4tJfYf2DAtY4tmwDv16wAurUIL7XH93pIEQ BOUnj/eISTITsALQZZtxwFXxNzY2FiIfMJ+8yCPaEFDo8LkjdGUAoLAcFms20BFzivye y8WPv+JIvMPm4CoxuHPETp4v+FLdQ7Fazk++drMaqM7rxPTirlCKNbVgMz3Oe/DZWkt9 zDWA== MIME-Version: 1.0 X-Received: by 10.58.97.238 with SMTP id ed14mr190407veb.34.1375832939042; Tue, 06 Aug 2013 16:48:59 -0700 (PDT) In-Reply-To: <59b6f7db-a1e6-4326-8c87-49f2678b7db5@googlegroups.com> References: <2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com> <59b6f7db-a1e6-4326-8c87-49f2678b7db5@googlegroups.com> Date: Tue, 6 Aug 2013 19:48:58 -0400 Subject: Re: HTTP post with urllib2 From: Joel Goldstick To: cerr Content-Type: text/plain; charset=UTF-8 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 128 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375832947 news.xs4all.nl 15902 [2001:888:2000:d::a6]:44398 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52090 On Tue, Aug 6, 2013 at 7:35 PM, cerr 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 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 >> >> > 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