ここに私のserver.pyがあります:
import BaseHTTPServer
import SocketServer
class TestRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
self.wfile.write("hello world at %s" % __file__)
server = BaseHTTPServer.HTTPServer(('', 10000), TestRequestHandler)
#server = SocketServer.ThreadingTCPServer(('', 8888), TestRequestHandler)
server.serve_forever()
ここに私のclient.pyがあります:
import urllib2
req = urllib2.Request('http://127.0.0.1:10000/')
handle = urllib2.urlopen(req)
content = handle.read()
次に、server.py を起動します。動作します。
client.py を起動すると、Windows 7、Python 2.6 で次のエラーが発生します。
Traceback (most recent call last):
File "D:\Dropbox\Forge\urllib-error\client.py", line 3, in <module>
handle = urllib2.urlopen(req)
File "C:\Python26\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python26\lib\urllib2.py", line 391, in open
response = self._open(req, data)
File "C:\Python26\lib\urllib2.py", line 409, in _open
'_open', req)
File "C:\Python26\lib\urllib2.py", line 369, in _call_chain
result = func(*args)
File "C:\Python26\lib\urllib2.py", line 1161, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\Python26\lib\urllib2.py", line 1136, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions>
ブラウザからhttp://127.0.0.1:10000/を開くと動作します。サーバーが正常であることを意味します。
client.py でhttp://127.0.0.1:10000/をhttp://127.0.0.1/またはhttp://127.0.0.1:80/に置き換えると、すべて正常に動作します (これはポート 80 の別の Web サーバーです) - アパッチ)。
私は何を間違っていますか?
UPD:このclient2.pyを使用すると同じエラーが発生します:
import urllib2
handle = urllib2.urlopen('http://127.0.0.1:10000/')
content = handle.read()
UPD:問題は解決しました。それは私のファイアウォールでした。無効にすると、すべてが機能します。バカ、バカな私。読んでくれてありがとう :-)