gpt4 book ai didi

google-app-engine - 从 GAE 请求时从 make_fetch_call 中删除 header (用户代理)

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:05 25 4
gpt4 key购买 nike

我有一个 Google App Engine(GAE) 应用程序,我使用的是 Python 2.7。此应用程序从用户门户(例如 Chrome)接收 GET(ajax) 请求。收到请求后,我准备异步连接以使用 urlfetch.make_fetch_call() - GET 请求从 GAE 外部的多个网站(比如 X1、X2 等)请求数据。

这适用于 X1 网站但不适用于 X2。 开始在本地开发服务器上进行探测。探测后我怀疑 X2 正在检查 header 中的 {'User-Agent':'Python-urllib/2.7'} 标记。这是我最好的猜测,因为将此字段更改为 {'User-Agent': 'Mozilla/5.0'} 会返回所需的结果。

所以我将代码上传到 GAE 并使用 urlfetch.make_fetch_call() 启动了该过程。拦截此调用后,我发现无论我做什么,都不会删除 GAE 添加的默认 header 。这是 GAE 添加的默认 header 。

302 218ms 0kb Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/48.0.2564.103 Safari/537.36 AppEngine-Google; (+ http://code.google.com/appengine ; appid: s~xxx-etching-112014) 模块=默认版本=1107.178.194.96 - - [06/Feb/2016:19:57:04 -0800] "GET/HTTP/1.1"302 383 "http://www.mywebbsite.com/ ""Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML , 像 Gecko) Chrome/48.0.2564.103 Safari/537.36 AppEngine-Google; (+ http://code.google.com/appengine ; appid: s~xxx-etching-112014)""1.usedForIntercepting.appspot.com"ms=218 cpu_ms=224 cpm_usd= 0.000043 loading_request=1 app_engine_release=1.9.32 trace_id=fd7b7420e7f8c23371a5b0ea7e9651 instance=00c61b117ce5ebac2a2eba44f26a01d4f2

这是我试过的

for portal in self.searchPortals:
spoofHeader = {
'User-agent':'Mozilla/5.0----------------------',
'Host':portal.getURL(),
'Accept-Encoding': 'identity',
'Connection': 'close',
'Accept': 'application/json, text/plain, */*',
'Origin': 'http://www.mywebsite.com'

}
logging.info(spoofHeader)
rpc = urlfetch.create_rpc(deadline=5)
rpc.callback = lambda: self.handleCallBack(rpc, portal)
#urlfetch.make_fetch_call(rpc, portal.getSearchURL(searchKeyword), headers={'User-agent':'Mozilla/5.0'})
urlfetch.make_fetch_call(rpc, url='http://1.usedforintercepting.appspot.com', headers=spoofHeader)
rpcs.append(rpc)

for rpc in rpcs:
rpc.wait()

这是我收到的。

2016-02-07 13:01:21.306/302 59ms 0kb Mozilla/5.0-------------------- AppEngine-Google; (+ http://code.google.com/appengine ; appid: s~xxx-etching-112014) 模块=默认版本=1107.178.194.20 - - [06/Feb/2016:23:31:21 -0800] "GET/HTTP/1.1"302 383 - "Mozilla/5.0---------------- ------ AppEngine-Google; (+ http://code.google.com/appengine ; appid: s~xxx-etching-112014)""1.usedForIntercepting.appspot.com"ms=59 cpu_ms=6 cpm_usd=0.000043 app_engine_release=1.9.32 trace_id=a4a1f521c5a6fa65ed0295835dd175 实例=00c61b117ce5ebac2a2eba44f26a01d4f2

我想要的是这样的。

获取 http://somelink/search/abc HTTP/1.1接受编码:身份主办单位:somelink.com连接:关闭用户代理:Mozilla/5.0

我想删除除 User-Agent:Mozilla/5.0 之外的所有表单标题 ??

注意 - 为了使用 urlfetch 拦截从 GAE 发出的请求,我正在使用另一个 GAE 实例。

最佳答案

在文档中,URL Fetch Python API Overview: Request Headers , 它说

For security reasons, the following headers cannot be modified by the application:

  • Content-Length
  • Host
  • Vary
  • Via
  • X-Appengine-Inbound-Appid
  • X-Forwarded-For
  • X-ProxyUser-IP

它还说:

The following headers indicate the app ID of the requesting app:

User-Agent. This header can be modified but App Engine will append an identifier string to allow servers to identify App Engine requests. The appended string has the format "AppEngine-Google; (+http://code.google.com/appengine; appid: APPID)", where APPID is your app's identifier.

如果您想要自定义 header ,则必须编写自己的 urlfetch 代码或使用外部服务器为您调用 header 。

关于google-app-engine - 从 GAE 请求时从 make_fetch_call 中删除 header (用户代理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35251014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com