gpt4 book ai didi

Python Socket 连接

转载 作者:行者123 更新时间:2023-12-03 12:09:22 24 4
gpt4 key购买 nike

你好我要Python Socket Connect Poloniex API .
我运行了代码。但我无法得到我想要的结果。
我做了代码:
==================================================== ==================

import requests
import socket

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect(("poloniex.com",443))

message="GET /public?command=returnTicker HTTP/1.1\r\nHost: poloniex.com\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.18.1\r\n\r\n"

s.send(message)

print s.recv(5000)
==================================================== ==================
响应文本:

HTTP/1.1 400 Bad Request

Server: cloudflare-nginx

Date: Tue, 20 Jun 2017 02:52:22 GMT

Content-Type: text/html

Content-Length: 275

Connection: close CF-RAY: - 400 The plain HTTP request was sent to HTTPS port


==================================================== ==================

最佳答案

错误消息是正确的 - 您正在向 HTTPS 端口 443 端口发送 HTTP 请求。如果要发送 HTTP 请求,请使用端口 80。我刚刚尝试向端口 80 发送请求,响应说我应该从现在开始使用 HTTPS(参见 Location: https:// 部分):

HTTP/1.1 301 Moved Permanently
Date: Tue, 20 Jun 2017 13:40:52 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d28a8f446379618a093014a5f13bbcb141497966052; expires=Wed, 20-Jun-18 13:40:52 GMT; path=/; domain=.poloniex.com; HttpOnly
Location: https://poloniex.com/public?command=returnTicker
Server: cloudflare-nginx
CF-RAY: 371f2473b09f5a7a-BOS

在这种情况下,您应该使用 ssl模块而不是 socket , 或直接使用 requests因为它是一个更简单的选择。

关于Python Socket 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44643075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com