Python Module: http.client

This time I will record something about Python Language, I am a new student in this aspect.

So lots of content is very fundamental and no creative.


Field: Http

I search lots of information about Http and Python something stuff,and the most consequent was [Httplib] and [Httplib2], I finally chose use [http.client] the module, which comes with Python3 and Python3 have a Doc explain the module.


#!/usr/bin/python
import http.client
import urllib.parse
conn = http.client.HTTPConnection("www.baidu.com")
# GET method
conn.request("GET", "/?tn=07084049_1_pg")
response = conn.getresponse()
print(response.status, response.reason) #
print(response.read(400)) # 400bytes
# HEAD method
conn.request("HEAD", "/?tn=07084049_1_pg")
print(response.status, response.reason) #
print(len(response.read()))
# POST request
params = urllib.parse.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}
conn = http.client.HTTPConnection("bugs.python.org")
conn.request("POST", "", params, headers)
response = conn.getresponse()
print(response.status, response.reason)
print(response.read())


你可能感兴趣的:(http,python)