Question

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
Était-ce utile?

La solution

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.

Autres conseils

Windows adds a "Newline" character in the end ('\n') which is different from unix Windows=CRLF. I was reading a Windows ASCII text file and ran into a similar issue. If I just read the file into a list and print it, it adds an empty line between the two. That gave me a clue. I was using Requests library which is built on top of urllib3.

If I use

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

it works just fine.

r = requests.get(url)

bombs out with errors

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top