首页 > 解决方案 > Python http Post 错误 getaddrinfo 失败

问题描述

我正在尝试读取文件,对其进行编码并将其附加到带有 HTTPS 帖子的基本身份验证中,但出现错误:getaddrinfo failed

读取文件

filePath = "R:\SSIS\Temp\import.json"
fileBytes = open(filePath, 'r', encoding='ISO-8859-1').read
import uuid
gu = uuid.uuid4
boundary = "--" + str(gu)

设置带有边界的表单数据

from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

related = MIMEMultipart('form-data', boundary)  # second argument is the boundary.
file_part = MIMEApplication(
    open(filePath, 'r', encoding='ISO-8859-1').read(),
)

file_part.add_header('Content-disposition', 'form-data; name="uploadedfile"')
related.attach(file_part)

body = related.as_string().split('\n\n', 1)[1]
headers = creds

做 POST

import http.client
c = http.client.HTTPSConnection("https://energytransfer.collibra.com/rest/2.0/import/json-job", 443)
r = c.request("POST", '/post', body, headers)
#get the response back
res = c.getresponse()

这是错误:

  File "d:\Junk\junk.py", line 38, in <module>
    r = c.request("POST", '/post', body, headers)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1253, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1299, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1248, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1008, in _send_output
    self.send(msg)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 948, in send
    self.connect()
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1415, in connect
    super().connect()
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 919, in connect
    self.sock = self._create_connection(
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\socket.py", line 822, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\socket.py", line 953, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

标签: python-3.x

解决方案


推荐阅读