Python如何实现从入门到精通,深入HTTP交互技巧?
- 内容介绍
- 文章标签
- 相关推荐
本文共计179个文字,预计阅读时间需要1分钟。
章节将展示如何使用Python原生库实现简单的HTTP请求,需要使用到requests、urllib和http.client库。
一、导入所需库
pythonimport requestsfrom urllib import parseimport http.client
本章会原生的库实现简单的Http调用,需要用到requests、urllib和pypi.python.org/pypi?:action=login', auth=('user','password'))import requests
url = 'pypi.python.org'
# First request
resp1 = requests.get(url)
# Second requests with cookies received on first requests
resp2 = requests.get(url, cookies=resp1.cookies)
二、Post
import requestsurl = 'www.python.org')
status = resp.status_code
x_timer = resp.headers['X-Timer']
content_type = resp.headers['Content-Type']
content_length = resp.headers['Content-Length']
四、FileUpload
import requestsurl = 'pypi.python.org','username','password')
opener = urllib.request.build_opener(auth)
r = urllib.request.Request('pypi.python.org/pypi?:action=login')
u = opener.open(r)
resp = u.read()
本文共计179个文字,预计阅读时间需要1分钟。
章节将展示如何使用Python原生库实现简单的HTTP请求,需要使用到requests、urllib和http.client库。
一、导入所需库
pythonimport requestsfrom urllib import parseimport http.client
本章会原生的库实现简单的Http调用,需要用到requests、urllib和pypi.python.org/pypi?:action=login', auth=('user','password'))import requests
url = 'pypi.python.org'
# First request
resp1 = requests.get(url)
# Second requests with cookies received on first requests
resp2 = requests.get(url, cookies=resp1.cookies)
二、Post
import requestsurl = 'www.python.org')
status = resp.status_code
x_timer = resp.headers['X-Timer']
content_type = resp.headers['Content-Type']
content_length = resp.headers['Content-Length']
四、FileUpload
import requestsurl = 'pypi.python.org','username','password')
opener = urllib.request.build_opener(auth)
r = urllib.request.Request('pypi.python.org/pypi?:action=login')
u = opener.open(r)
resp = u.read()

