簡體   English   中英

Python urllib2無法在備用端口(不是80)上打開localhost?錯誤10013

[英]Python urllib2 cannot open localhost on alternate port (not 80)? Error 10013

這是我的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/時 ,它可以工作。 這意味着服務器很好。

當我更換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()

UPD:問題解決了。 這是我的防火牆。 一旦禁用,一切正常。 愚蠢,愚蠢我。 謝謝閱讀 :-)

本地防火牆阻止了連接。 當它被禁用時,一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM