1

我正在使用 GAE 构建一个 Web 应用程序,该应用程序使用 urllib2 库获取 URL。

有人可以向我解释为什么这段代码:

import webapp2
import urllib2

class BaseHandler(webapp2.RequestHandler):      
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)


class MainPage(BaseHandler):
    def get(self):
        self.write(urllib2.urlopen("http://www.google.com").read())


app = webapp2.WSGIApplication([("/", MainPage)], debug = True)

在本地环境中访问时抛出以下异常,但在部署时按预期工作?

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\Developer\Desktop\test\main.py", line 15, in get
    self.write(urllib2.urlopen("http://www.google.com").read())
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 400, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 418, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\lib\urllib2.py", line 1180, in do_open
    r = h.getresponse(buffering=True)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\dist27\httplib.py", line 507, in getresponse
    'An error occured while connecting to the server: %s' % e)
error: An error occured while connecting to the server: Unable to fetch URL: http://www.google.com Error: [Errno 10106] getaddrinfo failed

请注意,当从解释器中使用 urllib2 时,它会按预期工作。

此外,如果使用 IP 地址完成获取,则应用程序将按预期工作。例如

self.write(urllib2.urlopen("http://173.194.42.34").read())

注意:我确实已经用另一个帐户问过这个问题,我没有注意到我正在用另一封邮件访问 SO,而不是我打算发送垃圾邮件。相反,这个问题更直接,代码+堆栈跟踪更能说明我的问题

4

0 回答 0