在Djang中创建ServertoServer调用API

2024-09-28 22:21:10 发布

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

    POST /somelink
    Host: hostname
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101
    Firefox/47.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate, br
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 683
    Cookie: xxx
    Connection: keep-alive
    access_token=xxx

如何使用这些细节形成api调用? 我已经试过了:

payload={ "POST":"somelink",
                         "Host":"hostname",
                         "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0",
                         "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                         "Accept-Language":"en-US,en;q=0.5",
                         "Accept-Encoding":"gzip, deflate, br",
                         "Content-Type": "application/x-www-form-urlencoded",
                         "Content-Length": "683",
                         "Cookie":" xxx",
                         "Connection":"keep-alive",
                         "access_token":"xxx"}
                headers = {}
                r = requests.post(url, data=json.dumps(payload), headers=headers)

但如果我这样做,我会得到“Connection aborted.', gaierror(-2, 'Name or service not known”。 有人能帮我吗?你知道吗


Tags: hostmozillaapplicationxmlcontentconnectionposthostname
1条回答
网友
1楼 · 发布于 2024-09-28 22:21:10

gaierror表示DNS查找失败:What does this socket.gaierror mean?

这意味着Python无法将主机名解析为IP地址来启动请求。示例:

>>> from socket import gethostbyname
>>> gethostbyname('www.google.com')
'172.217.26.196'
>>> gethostbyname('www.wrongurlthatdoesnotexist.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -2] Name or service not known

相关问题 更多 >