Python Send http post example


HTTPResponse instances have the following methods and attributes:

read()
Reads and returns the response body.


getheader(name[, default])
Get the contents of the header name, or default if there is no matching header.


msg
A mimetools.Message instance containing the response headers.


version
HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.


status
Status code returned by server.


reason
Reason phrase returned by server.


import httplib, urllib

params = urllib.urlencode({'acc': 'user', 'pw': 'pw'})
headers = {"Content-type": "application/x-www-form-urlencoded",
		   "Accept": "text/plain"}
conn = httplib.HTTPConnection("192.168.1.1")
conn.request("POST", "/m_login", params, headers)
response = conn.getresponse()
#print response.status, response.reason, response.msg

cookie = response.getheader("Set-Cookie")

data = response.read()
print "\r\n", data, "\r\n"
conn.close()

0 意見: