我正在使用http.client嘗試從主機讀取xml文件。我會使用urllib2,但是我得到一個BadStatusLine,因爲在xml頭部之前有3個空格(我無法改變它)。這就是爲什麼我正在嘗試這條路線。Python http.client getaddrinfo失敗
我現在卡住了,我不斷收到錯誤(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
嘗試刪除'的 'http://''您前面加上到IP。 – ejno
這工作,但現在我再次得到一個badstatusline?我無法更改我得到的文件,並且它通過瀏覽器正常工作。有任何想法嗎? – mad5245