http://www.oschina.net/code/snippet_54100_7485
[代码] GET 方法
02 | >>> conn = httplib.HTTPConnection( "www.python.org" ) |
03 | >>> conn.request( "GET" , "/index.html" ) |
04 | >>> r1 = conn.getresponse() |
05 | >>> print r1.status, r1.reason |
08 | >>> conn.request( "GET" , "/parrot.spam" ) |
09 | >>> r2 = conn.getresponse() |
10 | >>> print r2.status, r2.reason |
[代码] HEAD 方法
02 | >>> conn = httplib.HTTPConnection( "www.python.org" ) |
03 | >>> conn.request( "HEAD" , "/index.html" ) |
04 | >>> res = conn.getresponse() |
05 | >>> print res.status, res.reason |
[代码] POST 方法
01 | >>> import httplib, urllib |
02 | >>> params = urllib.urlencode({ 'spam' : 1 , 'eggs' : 2 , 'bacon' : 0 }) |
03 | >>> headers = { "Content-type" : "application/x-www-form-urlencoded" , |
04 | ... "Accept" : "text/plain" } |
05 | >>> conn = httplib.HTTPConnection( "musi-cal.mojam.com:80" ) |
06 | >>> conn.request( "POST" , "/cgi-bin/query" , params, headers) |
07 | >>> response = conn.getresponse() |
08 | >>> print response.status, response.reason |
10 | >>> data = response.read() |