GAE:Urllib2不适用于本地环境

时间:2013-03-19 13:45:52

标签: python google-app-engine urllib2

我正在使用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地址完成提取,则应用程序将按预期工作。 E.G。

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

注意:我确实问过this已经使用了另一个帐户,我没有注意到我正在使用其他邮件访问SO,而不是我打算发送垃圾邮件。相反,这个问题更简单,代码+堆栈跟踪更能解释我的问题

0 个答案:

没有答案