質問

テストしている簡単なWebサイトがあります。 localhostで実行されており、Webブラウザーでアクセスできます。インデックスページは、単に「実行中」という単語です。 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がピックアップしているプロキシ設定が定義されているように聞こえます。 &quot; 127.0.0.01/"をプロキシしようとすると、プロキシはあきらめて504エラーを返します。

Python urllib2プロキシを取得しないから:

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