服务器 python cant open file,Python urllib2无法在备用端口(不是80)上打开localhost?错误10013...

在Windows 7上,使用Python 2.6时,作者遇到一个错误:当尝试用urllib2从本地HTTP服务器获取数据时,遇到了URLError。问题在于本地防火墙阻止了连接。关闭防火墙后,服务器和客户端之间的通信恢复正常。
摘要由CSDN通过智能技术生成

Here is my 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()

Here is my client.py:

import urllib2

req = urllib2.Request('http://127.0.0.1:10000/')

handle = urllib2.urlopen(req)

content = handle.read()

Then I start server.py, it works.

When I start client.py, I get this error on Windows 7, Python 2.6:

Traceback (most recent call last):

File "D:\Dropbox\Forge\urllib-error\client.py", line 3, in

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:

When I open http://127.0.0.1:10000/ from a browser, it works. It means server is fine.

When I replace http://127.0.0.1:10000/ with http://127.0.0.1/ or http://127.0.0.1:80/ in client.py, everything works fine (this is another web server on port 80 - apache).

What do I do wrong?

UPD: Same error when I use this client2.py:

import urllib2

handle = urllib2.urlopen('http://127.0.0.1:10000/')

content = handle.read()

UPD: Problem solved. It was my firewall. When disabled, everything works. Stupid, stupid me. Thanks for reading :-)

解决方案

The local firewall prevented the connection. When it's disabled, everything works.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值