简体   繁体   English

如何在python中制作准确的用户代理字符串?

[英]How can I make an accurate user-agent string in python?

I want to test a wsgi application. 我想测试一个wsgi应用程序。 I want to make an accurate environ variable without making any socket connections or http calls. 我想在不进行任何套接字连接或http调用的情况下创建一个准确的environ变量。 (If my test framework is the server, and the app lives on the same machine, why make http calls?) (如果我的测试框架是服务器,并且应用程序位于同一台计算机上,为什么要进行http调用?)

I found webtest and am currently looking at it's source to figure it out (it's not working for google app engine apps for some reason). 我找到了webtest ,目前正在寻找它的来源(由于某种原因,它不适用于google app引擎应用)。

My current issue with making an accurate environ variable is the user-agent. 我当前与准确的环境变量有关的问题是用户代理。 I can get system information with import platform; platform.uname() 我可以通过import platform; platform.uname()获取系统信息import platform; platform.uname() import platform; platform.uname() , but it returns Darwin for Macbooks and not Macintosh , which is found in a user-agent string when sent from a macbook. import platform; platform.uname() ,但它返回Macbook而不是Macintosh Darwin ,这是从macbook发送时在user-agent字符串中找到的。 I've found multiple libraries to generate 'fake' user-agents, but I want to make the real one. 我发现有多个库可以生成“伪”用户代理,但我想制作一个真正的库

If you're using Google App Engine, and the issue is having an accurate user agent from within a test , first thing is you'll probably want to use App Engine Testbed . 如果您在使用谷歌应用程序引擎,并且该问题具有从测试中的准确的用户代理,第一件事情是,你可能会想使用App Engine的测试平台 From there there are special instructions on using webtest . 从那里有使用webtest的特殊说明。 Unless you're doing something weird, that should work just fine. 除非您做的事情很怪异,否则应该没问题。

The way then to pass custom headers, including user agent, is like this ( per the docs ): 然后传递自定义标头(包括用户代理)的方式是这样的( 根据docs ):

app.get('/path', [params], [headers], [extra_environ], ...)

So for example: 因此,例如:

app.get('/foo', headers={'User-Agent': 'Mozilla'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM