这是我的服务器.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()
这是我的客户端.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/从浏览器中,它可以工作。这说明服务器没问题。
当我更换时http://127.0.0.1:10000/ with http://127.0.0.1/ or http://127.0.0.1:80/在 client.py 中,一切正常(这是端口 80 上的另一个 Web 服务器 - apache)。
我做错了什么?
UPD:当我使用这个时出现同样的错误客户端2.py:
import urllib2
handle = urllib2.urlopen('http://127.0.0.1:10000/')
content = handle.read()
UPD:问题解决了。这是我的防火墙。禁用后,一切正常。愚蠢的我,愚蠢的我。谢谢阅读 :-)