我正在使用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
所以我想出来了。为了避免badStatusLines和其他类似的错误,我使用了sockets/urllib2。这样你就可以从网页上获取原始信息,而不必担心任何你无法控制的问题。
下面是添加了套接字的代码片段。
这是我迄今为止唯一的成功。感谢ejno让我走上正轨。
Windows在结尾('\n')添加了一个“换行”字符,这与unix Windows=CRLF不同。我在读一个Windows ASCII文本文件时遇到了类似的问题。如果我只是将文件读入列表并打印出来,它会在两者之间添加一个空行。这给了我一个线索。我使用的是建立在urllib3之上的请求库。
如果我用
它工作得很好。
误炸
相关问题 更多 >
编程相关推荐