2013-06-04 81 views
1

我正在使用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 
+1

嘗試刪除'的 'http://''您前面加上到IP。 – ejno

+0

這工作,但現在我再次得到一個badstatusline?我無法更改我得到的文件,並且它通過瀏覽器正常工作。有任何想法嗎? – mad5245

回答

1

所以我想通了。爲了避免badStatusLines和其他類似的錯誤,我使用了socket/urllib2。這樣你就可以從網頁獲得原始信息,而不必擔心任何你無法控制的問題。

這裏是添加了套接字的代碼片段。

socket.setdefaulttimeout(timeout) 
req = urllib2.Request(host) 
response = urllib2.urlopen(req) 

這是迄今爲止我發現的唯一成功。感謝ejno讓我走上正軌。

1

Windows在最後('\ n')添加了一個「新行」字符,它與unix Windows = CRLF不同。我正在閱讀Windows ASCII文本文件並遇到類似的問題。如果我只是將文件讀入列表並打印出來,它會在兩者之間添加一條空行。這給了我一個線索。我使用的是建立在urllib3之上的Requests庫。

如果我使用

r = requests.get(url.strip('\n')) 

它工作得很好。

r = requests.get(url) 

彈出有錯誤

Traceback (most recent call last): File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.p y", line 516, in urlopen body=body, headers=headers) File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.p y", line 308, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\Python34\lib\http\client.py", line 1090, in request self._send_request(method, url, body, headers) File "C:\Python34\lib\http\client.py", line 1128, in _send_request self.endheaders(body) File "C:\Python34\lib\http\client.py", line 1086, in endheaders self._send_output(message_body) File "C:\Python34\lib\http\client.py", line 924, in _send_output self.send(msg) File "C:\Python34\lib\http\client.py", line 859, in send self.connect() File "C:\Python34\lib\site-packages\requests\packages\urllib3\connection.py", line 146, in connect conn = self._new_conn() File "C:\Python34\lib\site-packages\requests\packages\urllib3\connection.py", line 125, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "C:\Python34\lib\site-packages\requests\packages\urllib3\util\connection. py", line 64, in create_connection for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): File "C:\Python34\lib\socket.py", line 530, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed

相關問題