Python 3从URL读取数据

时间:2015-12-02 20:29:45

标签: python xml linux python-3.x urllib

当我想从URL

中读取时,我遇到了Python 3的问题
URL="http://"+USER+":"+PASSWORD+"@127.0.0.1:"+PORT+"/manager/status?XML=true"
RURL=urllib.request.urlopen(URL)
XML=RURL.read()
RURL.close()

在较旧的Python版本中,以下工作正常:

URL="http://"+USER+":"+PASSWORD+"@127.0.0.1:"+PORT+"/manager/status?XML=true"
RURL=urllib.urlopen(URL)
XML=RURL.read()
RURL.close()

现在,当我运行脚本时,我得到以下内容:

Traceback (most recent call last):
  File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 1248, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/opt/rh/python33/root/usr/lib64/python3.3/http/client.py", line 1061, in request
    self._send_request(method, url, body, headers)
  File "/opt/rh/python33/root/usr/lib64/python3.3/http/client.py", line 1099, in _send_request
    self.endheaders(body)
  File "/opt/rh/python33/root/usr/lib64/python3.3/http/client.py", line 1057, in endheaders
    self._send_output(message_body)
  File "/opt/rh/python33/root/usr/lib64/python3.3/http/client.py", line 902, in _send_output
    self.send(msg)
  File "/opt/rh/python33/root/usr/lib64/python3.3/http/client.py", line 840, in send
    self.connect()
  File "/opt/rh/python33/root/usr/lib64/python3.3/http/client.py", line 818, in connect
    self.timeout, self.source_address)
  File "/opt/rh/python33/root/usr/lib64/python3.3/socket.py", line 417, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./Tomcatxml.py", line 15, in <module>
    RURL=urllib.request.urlopen(URL)
  File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 156, in urlopen
    return opener.open(url, data, timeout)
  File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 469, in open
    response = self._open(req, data)
  File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 487, in _open
    '_open', req)
  File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 447, in _call_chain
    result = func(*args)
  File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 1268, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 1251, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>

0 个答案:

没有答案