I am using http.client to try to read an xml file from a host. I would use urllib2, but I get a BadStatusLine because there is 3 spaces before the xml header(I can not change that). That is why I am trying this route.
I am stuck now and I keep getting an error (getaddrinfo failed).
Below is my code and below that is the traceback. Can someone advise what I am doing wrong?
FYI the address that works on the browser is http://machineIP:81/command=AB&time=2013-06-02
I have no problem accessing the xml that way.
Thanks for any help in advance!
Code:
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.")
Traceback:
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
So I figured it out. To avoid badStatusLines and other similar errors I used sockets/urllib2. That way you can get the raw info from the webpage and not have to worry about any issues you can not control.
here is the snippet of code with sockets added.
socket.setdefaulttimeout(timeout)
req = urllib2.Request(host)
response = urllib2.urlopen(req)
This was the only success I found so far. Thanks to ejno for getting me on the right track.