문제

테스트중인 간단한 웹 사이트가 있습니다. LocalHost에서 실행 중이며 웹 브라우저에서 액세스 할 수 있습니다. 색인 페이지는 단순히 "running"이라는 단어입니다. urllib.urlopen 페이지를 성공적으로 읽지 만 urllib2.urlopen 하지 않을 것이다. 다음은 문제를 보여주는 스크립트입니다 (이것은 실제 스크립트이며 다른 테스트 스크립트의 단순화가 아닙니다) :

import urllib, urllib2
print urllib.urlopen("http://127.0.0.1").read()  # prints "running"
print urllib2.urlopen("http://127.0.0.1").read() # throws an exception

스택 추적은 다음과 같습니다.

Traceback (most recent call last):
  File "urltest.py", line 5, in <module>
    print urllib2.urlopen("http://127.0.0.1").read()
  File "C:\Python25\lib\urllib2.py", line 121, in urlopen
    return _opener.open(url, data)
  File "C:\Python25\lib\urllib2.py", line 380, in open
    response = meth(req, response)
  File "C:\Python25\lib\urllib2.py", line 491, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python25\lib\urllib2.py", line 412, in error
    result = self._call_chain(*args)
  File "C:\Python25\lib\urllib2.py", line 353, in _call_chain
    result = func(*args)
  File "C:\Python25\lib\urllib2.py", line 575, in http_error_302
    return self.parent.open(new)
  File "C:\Python25\lib\urllib2.py", line 380, in open
    response = meth(req, response)
  File "C:\Python25\lib\urllib2.py", line 491, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python25\lib\urllib2.py", line 418, in error
    return self._call_chain(*args)
  File "C:\Python25\lib\urllib2.py", line 353, in _call_chain
    result = func(*args)
  File "C:\Python25\lib\urllib2.py", line 499, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 504: Gateway Timeout

어떤 아이디어? 나는 더 고급 된 기능 중 일부가 필요할 수 있습니다. urllib2, 그래서 나는 단지 사용에 의지하고 싶지 않습니다. urllib, 또한이 문제를 이해하고 싶습니다.

도움이 되었습니까?

해결책

urllib2가 픽업되고 있음을 정의한 프록시 설정이있는 것처럼 들립니다. "127.0.0.01/"프록시를하려고 할 때 프록시는 포기하고 504 오류를 반환합니다.

에서 모호한 Python urllib2 프록시 gotcha:

proxy_support = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy_support)
print opener.open("http://127.0.0.1").read()

# Optional - makes this opener default for urlopen etc.
urllib2.install_opener(opener)
print urllib2.urlopen("http://127.0.0.1").read()

다른 팁

urlib2.open을 먼저 호출하고 urllib.open이 동일한 결과를 얻습니까? 첫 번째 열린 호출이 HTTP 서버가 타임 아웃을 일으키는 데 바쁘게 지낼 수 있는지 궁금하십니까?

무슨 일이 일어나고 있는지 모르겠지만,이를 알아내는 데 도움이 될 수 있습니다.

>>> import urllib2
>>> urllib2.urlopen('http://mit.edu').read()[:10]
'<!DOCTYPE '
>>> urllib2._opener.handlers[1].set_http_debuglevel(100)
>>> urllib2.urlopen('http://mit.edu').read()[:10]
connect: (mit.edu, 80)
send: 'GET / HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: mit.edu\r\nConnection: close\r\nUser-Agent: Python-urllib/2.5\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Tue, 14 Oct 2008 15:52:03 GMT
header: Server: MIT Web Server Apache/1.3.26 Mark/1.5 (Unix) mod_ssl/2.8.9 OpenSSL/0.9.7c
header: Last-Modified: Tue, 14 Oct 2008 04:02:15 GMT
header: ETag: "71d3f96-2895-48f419c7"
header: Accept-Ranges: bytes
header: Content-Length: 10389
header: Connection: close
header: Content-Type: text/html
'<!DOCTYPE '

urllib.urlopen ()은 서버에서 다음 요청을 던졌습니다.

GET / HTTP/1.0
Host: 127.0.0.1
User-Agent: Python-urllib/1.17

urllib2.urlopen ()이 이것을 던지는 동안 :

GET / HTTP/1.1
Accept-Encoding: identity
Host: 127.0.0.1
Connection: close
User-Agent: Python-urllib/2.5

따라서 서버는 HTTP/1.1 또는 추가 헤더 필드를 이해하지 못합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top