【问题标题】:Server error with using urllib2 on Google AppEngine在 Google AppEngine 上使用 urllib2 时出现服务器错误
【发布时间】:2013-10-28 06:55:54
【问题描述】:

我不确定为什么在向表单提交任何查询时,在 Google AppEngine 上托管此简单代码会返回服务器错误。问题似乎出在 html = urllib2.urlopen("http://google.com/search?q=" + q).read() 行上,因为没有它,代码也能正常工作。

import webapp2
import urllib2


form="""
<form action="/process">
    <input name="q">
    <input type="submit">
</form>
"""


class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write(form)


class ProcessHandler(webapp2.RequestHandler):
    def get(self):
        q = self.request.get("q")
        html = urllib2.urlopen("http://google.com/search?q=" + q).read()
        self.response.out.write(html)


app = webapp2.WSGIApplication([('/', MainHandler),
                               ('/process', ProcessHandler)],
                               debug=True)

这是返回的错误:

Error: Server Error
The server encountered an error and could not complete your request.

If the problem persists, please report your problem and mention this error message and the query that caused it.

【问题讨论】:

    标签: python html google-app-engine python-2.7 urllib2


    【解决方案1】:

    可能 www.google.com 不接受这种直接连接,取消来自特定用户代理的连接。在简单的 python 环境中,您可以更改用户代理字符串,但我认为通过 google 应用引擎无法做到这一点。

    【讨论】:

    • DVincenteR :对不起,我是 web-dev 的新手。这里的用户代理是什么?
    • Http 连接使用“User-Agent”标头标识发起请求的客户端软件。默认情况下,在 urllib2 中类似于“Python-urllib/2.6”。您可以设置一个不同的字符串并尝试显示为 chrome 或 firefos,但我不确定 google 是否允许用户代理更改。
    【解决方案2】:

    Google 正在向您的搜索字符串返回 403

    >>> import urllib2
    >>> html = urllib2.urlopen("http://google.com/search?q=Test").read()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
        return _opener.open(url, data, timeout)
      File "/usr/lib/python2.7/urllib2.py", line 410, in open
        response = meth(req, response)
      File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
        'http', request, response, code, msg, hdrs)
      File "/usr/lib/python2.7/urllib2.py", line 442, in error
        result = self._call_chain(*args)
      File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
        result = func(*args)
      File "/usr/lib/python2.7/urllib2.py", line 629, in http_error_302
        return self.parent.open(new, timeout=req.timeout)
      File "/usr/lib/python2.7/urllib2.py", line 410, in open
        response = meth(req, response)
      File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
        'http', request, response, code, msg, hdrs)
      File "/usr/lib/python2.7/urllib2.py", line 448, in error
        return self._call_chain(*args)
      File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
        result = func(*args)
      File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 403: Forbidden
    

    但是这可行:

    html = urllib2.urlopen("http://google.com").read()

    所以看起来谷歌正试图阻止这种搜索。正如另一位发帖人所建议的那样,更改用户代理字符串可能会停止 403。选择一些共同点!

    我刚刚使用 Mozilla 用户代理集进行了测试,我可以得到我认为您正在寻找的结果

    import urllib2
    headers = { 'User-Agent' : 'Mozilla/5.0' }
    req = urllib2.Request('http://google.com/search?q=Test', None, headers)
    html = urllib2.urlopen(req).read()
    print html
    

    【讨论】:

    • 谢谢。我是 web-dev 及其行话的新手。您能在这里简单描述一下什么是用户代理吗?谢谢。
    • 嗨 Ankit,用户代理是一个请求标头,用于帮助识别发出请求的内容。我添加了一个示例,可以让您获得我相信您所追求的结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    相关资源
    最近更新 更多