2 weixin 40215295 weixin_40215295 于 2017.09.12 22:45 提问

【急求解答】Python http进行url访问时报错

代码部分如下(MyPython.py):
r_ip = "xxx.xxx.xxx.xxx"
r_port = 8080
url = 'http://xxx.xxx.xxx.xxx:8080/aop/rest?'
geturl = url+urlPara.decode('gbk').encode('utf-8')
print("geturl=[%s]" % geturl)

conn = httplib.HTTPConnection(r_ip, r_port)
conn.request("GET", geturl)
response = conn.getresponse()
data = response.read()
print("data=[%s]" % data)
conn.close()

执行MyPython.py报错如下:
Traceback (most recent call last):
File "MyPythonTest.py", line 198, in
do_work()
File "MyPythonTest.py", line 182, in do_work
conn.request("GET", geturl)
File "/opt/freeware/lib/python2.7/httplib.py", line 973, in request
self._send_request(method, url, body, headers)
File "/opt/freeware/lib/python2.7/httplib.py", line 1007, in _send_request
self.endheaders(body)
File "/opt/freeware/lib/python2.7/httplib.py", line 969, in endheaders
self._send_output(message_body)
File "/opt/freeware/lib/python2.7/httplib.py", line 829, in _send_output
self.send(msg)
File "/opt/freeware/lib/python2.7/httplib.py", line 791, in send
self.connect()
File "/opt/freeware/lib/python2.7/httplib.py", line 772, in connect
self.timeout, self.source_address)
File "/opt/freeware/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 78] A remote host did not respond within the timeout period.

才开始学习Python不久,对其不太明白,还请大师解答。

3个回答

caozhy
caozhy   Ds   Rxr 2017.09.13 08:55

超时没有响应,你的地址端口是否正确,网络能否连上。

weixin_40215295
weixin_40215295 地址和端口都是正确的,这个应该是没有问题。我使用windows浏览器访问我生成的URL是可以的
2 个月之前 回复
oyljerry
oyljerry   Ds   Rxr 2017.09.13 09:46

你访问的URL是否正确,打印出来看看,然后看浏览器等是不是可以打开。

oyljerry
oyljerry 那你要看你的程序在的机器能不能访问
2 个月之前 回复
weixin_40215295
weixin_40215295 我在windows浏览器访问我生成的URL是可以的。这个应该是没有问题的。
2 个月之前 回复
weixin_40215295
weixin_40215295   2017.09.13 13:43

补充一下,我在windows浏览器访问我生成的URL是没有问题的,返回以下信息:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

  • xzqh海南省海口市伟大航路
  • app_keytest
  • formatxml
  • jkssl1
  • xmmxs{"xmmxs": {"byzd2": "", "byzd3": "", "byzd1": "", "sl": 1, "je": 100.0, "srxmbm": "HO2681"}}
  • timestamp20170912204259
  • sign68D2F2C4A0322554DF9D18F9DCFA0A88
  • v1.0
  • zje100.27
  • fkrzh6223876373338228
  • methodxxxx.xxxx.duizhang
  • sign_v1
  • kprq20170901
  • ylxtjyls20170901155012000001
  • skrq20170901
  • zsdwbm882000091

  • skzh
    28722229199129622810


    25
    ϞЧǩĻ
    C5A05FA08655F65310482C8D285431F8
    20170912205233

    这个信息就是我所需要的。
    另外,我ping了下IP,结果一直没有响应。我访问的IP地址是116.228.xxx.xxx,与我AIX服务器IP(192.168.xxx.xxx)不在一个网段,是否与防火墙之类的有关系?请大师提供个思路。

Csdn user default icon
上传中...
上传图片
插入图片