Python urllib2无法在备用端口(不是80)上打开localhost?错误10013

Ser*_*yev 3 python error-handling localhost urllib2 httplib

这是我的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()
Run Code Online (Sandbox Code Playgroud)

这是我的client.py:

import urllib2
req = urllib2.Request('http://127.0.0.1:10000/')
handle = urllib2.urlopen(req)
content = handle.read()
Run Code Online (Sandbox Code Playgroud)

然后我启动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>
Run Code Online (Sandbox Code Playgroud)

当我从浏览器打开http://127.0.0.1:10000/时,它可以工作.这意味着服务器很好.

当我更换http://127.0.0.1:10000/http://127.0.0.1/http://127.0.0.1:80/在client.py,一切工作正常(这是端口80上的另一个Web服务器 - 阿帕奇).

我做错了什么?

UPD:我使用此client2.py时出现相同的错误:

import urllib2
handle = urllib2.urlopen('http://127.0.0.1:10000/')
content = handle.read()
Run Code Online (Sandbox Code Playgroud)

UPD:问题解决了.这是我的防火墙.一旦禁用,一切正常.愚蠢,愚蠢我.谢谢阅读 :-)

phi*_*hag 5

本地防火墙阻止了连接.当它被禁用时,一切正常.