如何使用 python 的 urllib 设置标题? [英] How do I set headers using python's urllib?

查看:27
本文介绍了如何使用 python 的 urllib 设置标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 python 的 urllib 很陌生.我需要做的是为发送到服务器的请求设置一个自定义标头.具体来说,我需要设置 Content-type 和 Authorizations 标头.我查看了 python 文档,但找不到.

I am pretty new to python's urllib. What I need to do is set a custom header for the request being sent to the server. Specifically, I need to set the Content-type and Authorizations headers. I have looked into the python documentation, but I haven't been able to find it.

推荐答案

使用 添加 HTTP 标头urllib2:

来自文档:

import urllib2
req = urllib2.Request('http://www.example.com/')
req.add_header('Referer', 'http://www.python.org/')
resp = urllib2.urlopen(req)
content = resp.read()

这篇关于如何使用 python 的 urllib 设置标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆