是否有一个标准函数可以将 http header 转换为 python 字典,然后再转换回来?
当然,他们需要支持标题折叠。
最佳答案
我会使用 httplib 而不是使用套接字等构建自己的因此将从 http 服务器获取数据并将 header 解析为字典 例如
import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()
dict = r1.getheaders()
print(dict)
给予
[('content-length', '16788'), ('accept-ranges', 'bytes'), ('server', 'Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 mod_ssl/2.2.9 OpenSSL/0.9.8g mod_wsgi/2.5 Python/2.5.2'), ('last-modified', 'Mon, 15 Feb 2010 07:30:46 GMT'), ('etag', '"105800d-4194-47f9e9871d580"'), ('date', 'Mon, 15 Feb 2010 21:34:18 GMT'), ('content-type', 'text/html')]
以及用于将字典作为请求的一部分发送的方法。
关于python - 将 http header (字符串)转换为 python 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2268724/