我正在使用 GAE 构建一个简单的应用程序,该应用程序获取 URL 并使用 urllib2 进行一些处理,如文档中所述。
import webapp2
import urllib2
class Handler(webapp2.RequestHandler):
def get(self):
url = "http://www.google.com/"
try:
result = urllib2.urlopen(url)
self.response.out.write(result)
except urllib2.URLError, e:
handleError(e)
app = webapp2.WSGIApplication([("/", Handler)], debug = True)
对 urlfetch 进行了同样的尝试,但我一直收到同样的错误。
如果我使用 IP 地址调用 urlopen(),它工作正常。
我认为这可能是 DNS 问题,但从urllib2.urlopen("http://www.google.com")
解释器调用正确解决了。此外,我可以毫无问题地浏览和 ping。
请一些朋友在他们的环境中为我尝试这段代码,结果是一样的。
运行相同代码的已部署应用程序运行良好。
我想知道这是否可能是我的 ISP 的问题,因为所有 3 次尝试都是从同一个 ISP 的不同地方完成的。
不,我不会通过代理,至少不是我配置的或我能看到的。
这是堆栈跟踪
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 8, in get
result = urllib2.urlopen(url)
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