我正在使用 http.client 尝试从主机读取 xml 文件。我会使用 urllib2,但我得到一个 BadStatusLine,因为 xml 标头前有 3 个空格(我无法更改)。这就是我尝试这条路线的原因。
我现在被卡住了,并且不断收到错误消息(getaddrinfo 失败)。
下面是我的代码,下面是回溯。有人可以告诉我我做错了什么吗?
仅供参考,在浏览器上工作的地址是http://machineIP:81/command=AB&time=2013-06-02
我以这种方式访问 xml 没有问题。
提前感谢您的帮助!
代码:
import http.client
import datetime
IP = input("Enter the IP: ")
PT = str(81)
F1 = datetime.date.today() - datetime.timedelta(days=2)
print("Reading File...")
html = http.client.HTTPConnection('http://' + IP , port= PT)
html.request("GET", '/command=AB&time=' + str(F1))
r1 = html.getresponse()
print("Writing to file...")
out = r1.read()
f = open('Files/' + IP + '-' + str(F1) + '.xml', 'wb')
print("Writing to file...")
f.write(out)
f.close()
print("Done.")
追溯:
C:\Users\Me\Desktop\Coding>python file.py
Enter the IP: *.***.***.***
Reading File...
Traceback (most recent call last):
File "file.py", line 10, in <module>
html.request("GET", '/command=AB&time=' + str(F1))
File "C:\Python33\lib\http\client.py", line 1049, in request
self._send_request(method, url, body, headers)
File "C:\Python33\lib\http\client.py", line 1087, in _send_request
self.endheaders(body)
File "C:\Python33\lib\http\client.py", line 1045, in endheaders
self._send_output(message_body)
File "C:\Python33\lib\http\client.py", line 890, in _send_output
self.send(msg)
File "C:\Python33\lib\http\client.py", line 828, in send
self.connect()
File "C:\Python33\lib\http\client.py", line 806, in connect
self.timeout, self.source_address)
File "C:\Python33\lib\socket.py", line 406, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed