[英]Testing for user agent spoofing in python
I'm new to python and using python 3. I'm trying to download a webpage and what I want to know is if there is a way to actually see the name of the user agent the way a system admin or google sees it. 我是python的新手,并且正在使用python3。我正在尝试下载一个网页,我想知道的是,是否有一种方法可以像系统管理员或google一样实际查看用户代理的名称。 In my code I download and save the webpage to a text file like this :
在我的代码中,我将网页下载并保存到这样的文本文件中:
#Import
from urllib.request import urlopen,Request
url1 = urlopen(Request(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}))
#Create file and write
f=open('mah_a.txt','wb')
f.write(url1.read())
f.close()
How do I check to see if my user agent name has changed ? 如何检查我的用户代理名称是否已更改?
You changed the User-Agent header, yes. 您更改了User-Agent标头,是的。 You can use a online echo server like
httpbin.org
if you want to see what a server received: 如果要查看服务器收到的内容,可以使用诸如
httpbin.org
类的在线回显服务器:
url = 'http://httpbin.org/get'
url1 = urlopen(Request(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}))
print(url1.read().decode('utf8'))
Demo: 演示:
>>> from urllib.request import urlopen,Request
>>> url = 'http://httpbin.org/get'
>>> url1 = urlopen(Request(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}))
>>> print(url1.read().decode('utf8'))
{
"args": {},
"headers": {
"Accept-Encoding": "identity",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36"
},
"origin": "188.29.165.166",
"url": "http://httpbin.org/get"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.