【发布时间】:2016-12-29 15:27:54
【问题描述】:
我正在寻找一种在 Python 3 中解析 http 请求的本地方法。
This question 展示了一种在 Python 2 中执行此操作的方法,但使用了现已弃用的模块(和 Python 2),我正在寻找一种在 Python 3 中执行此操作的方法。
我主要想弄清楚请求的资源并解析标题和一个简单的请求。 (即):
GET /index.html HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
有人可以告诉我解析此请求的基本方法吗?
【问题讨论】:
-
你的第一句话表明你知道你应该只使用一个库(例如
urllib3,requests)。然后你说你试图在 Python 3 中做到这一点,但不知道怎么做。为什么不直接使用requests? -
@JonathonReinhart 我在不允许使用第三方库的环境中工作。
-
urllib 不是第三方
-
而且标准库中的这个类会做你想做的事。 docs.python.org/3/library/…
-
@cricket_007 他没有提到
urllib。他提到了第三方urllib3。
标签: python python-3.x http