关于python的HTTP请求方式
1. 域名解析
2. 发起TCP的3次握手
3. 建立TCP连接后发起http请求
4. 服务器端响应http请求,浏览器得到html代码
5. 浏览器解析html代码,并请求html代码中的资源
6. 浏览器对页面进行渲染呈现给用户
DNS解析(浏) ->TCP连接(三次握手)->http Requests(浏)->Response(服) -> Parse(浏)-> Render(浏)->TCP断开(四次挥手)
Python中将这些步骤被封装成了完整的模块,直接调用即可进行。
以python3为例,类似模块有:urllib,urllib3,httplib,requests等
get | post | |
---|---|---|
requests | Data = requests.get(url) | Data = requests.post(url,data =data) |
urllib | f = urllib.request.urlopen(url)f.read().decode('utf-8') | 类似get,在urlopen加入post提交的data |
urllib3 | http=urllib3.PoolManager()r=http.request(‘get’,url,fields={‘ ’:’ ’},headers={}) | get变为post |
httplib2 | h = httplib2.Http() head, content=h.request(url) | 在request函数中加设置请求方式post,以及提交的表单 |
pycurl | c = pycurl.Curl()c.setopt(c.URL, url)b = StringIO.StringIO()c.setopt(c.WRITEFUNCTION, b.write)c.perform() print b.getvalue() | curl.setopt(pycurl.POSTFIELDS, urllib.urlencode(data)) |
https://www.cnblogs.com/gaoyuechen/p/8245489.html
https://www.cnblogs.com/Lands-ljk/p/5447127.html
https://www.cnblogs.com/itech/archive/2011/01/10/1931946.html