Python 套接字连接 poloniex

Python Socket Connect poloniex

嗨,我想要 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

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