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

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

问题描述

我对Python的urllib很新。我需要做的是为请求发送到服务器设置一个自定义头。具体来说,我需要设置Content-type和Authorizations标头。

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头文件href =https://docs.python.org/2/library/urllib2.html =noreferrer> urllib2

docs:

from the docs:

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天全站免登陆