Python http.client getaddrinfo失败

2024-04-19 10:03:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: inpyselfipclientsendhttprequest
2条回答

所以我想出来了。为了避免badStatusLines和其他类似的错误,我使用了sockets/urllib2。这样你就可以从网页上获取原始信息,而不必担心任何你无法控制的问题。

下面是添加了套接字的代码片段。

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

这是我迄今为止唯一的成功。感谢ejno让我走上正轨。

Windows在结尾('\n')添加了一个“换行”字符,这与unix Windows=CRLF不同。我在读一个Windows ASCII文本文件时遇到了类似的问题。如果我只是将文件读入列表并打印出来,它会在两者之间添加一个空行。这给了我一个线索。我使用的是建立在urllib3之上的请求库。

如果我用

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

相关问题 更多 >