Python/cURL: Unterschied zwischen den Versionen
Aus SchnallIchNet
< Python
Cbs (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „== pycurl (cURL for python) == import pycurl c = pycurl.Curl() c.setopt(pycurl.URL, "http://www.python.org/") c.setopt(pycurl.HTTPHEADER, ["Accept:"]) b = S…“) |
Cbs (Diskussion | Beiträge) |
||
(Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt) | |||
Zeile 2: | Zeile 2: | ||
import pycurl | import pycurl | ||
+ | import StringIO | ||
+ | |||
+ | b = StringIO.StringIO() | ||
+ | |||
c = pycurl.Curl() | c = pycurl.Curl() | ||
c.setopt(pycurl.URL, "http://www.python.org/") | c.setopt(pycurl.URL, "http://www.python.org/") | ||
c.setopt(pycurl.HTTPHEADER, ["Accept:"]) | c.setopt(pycurl.HTTPHEADER, ["Accept:"]) | ||
− | |||
c.setopt(pycurl.WRITEFUNCTION, b.write) | c.setopt(pycurl.WRITEFUNCTION, b.write) | ||
c.setopt(pycurl.FOLLOWLOCATION, 1) | c.setopt(pycurl.FOLLOWLOCATION, 1) | ||
c.setopt(pycurl.MAXREDIRS, 5) | c.setopt(pycurl.MAXREDIRS, 5) | ||
c.perform() | c.perform() | ||
− | |||
+ | print b.getvalue() | ||
print c.getinfo(pycurl.HTTP_CODE), c.getinfo(pycurl.EFFECTIVE_URL) | print c.getinfo(pycurl.HTTP_CODE), c.getinfo(pycurl.EFFECTIVE_URL) | ||
+ | |||
+ | |||
+ | == urlencode == | ||
+ | |||
+ | perhaps you will need url-encoding for curl: | ||
+ | |||
+ | import urllib | ||
+ | f = { 'eventName' : 'myEvent', 'eventDescription' : "cool event"} | ||
+ | urllib.urlencode(f) | ||
+ | |||
+ | or simply use: | ||
+ | |||
+ | urllib.urlencode( {'eventName' : 'myEvent', 'eventDescription' : "cool event"} ) | ||
+ | |||
+ | '''Output:''' | ||
+ | |||
+ | eventName=myEvent&eventDescription=cool+event |
Aktuelle Version vom 11. August 2011, 07:36 Uhr
pycurl (cURL for python)
import pycurl import StringIO b = StringIO.StringIO() c = pycurl.Curl() c.setopt(pycurl.URL, "http://www.python.org/") c.setopt(pycurl.HTTPHEADER, ["Accept:"]) c.setopt(pycurl.WRITEFUNCTION, b.write) c.setopt(pycurl.FOLLOWLOCATION, 1) c.setopt(pycurl.MAXREDIRS, 5) c.perform() print b.getvalue() print c.getinfo(pycurl.HTTP_CODE), c.getinfo(pycurl.EFFECTIVE_URL)
urlencode
perhaps you will need url-encoding for curl:
import urllib f = { 'eventName' : 'myEvent', 'eventDescription' : "cool event"} urllib.urlencode(f)
or simply use:
urllib.urlencode( {'eventName' : 'myEvent', 'eventDescription' : "cool event"} )
Output:
eventName=myEvent&eventDescription=cool+event