pythonsocketspoloniex

Python Socket Connect poloniex


Hi I Want Python Socket Connect Poloniex API.

I ran the code. But I can not get the results I want.

I Made Code:

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

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)

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

Response Text:

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

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


Solution

  • The error message is right - you're sending an HTTP request to port 443 which is the HTTPS port. If you want to send an HTTP request, use port 80. I have just tried to send a request to port 80, and the response says I should be using HTTPS from now on (see Location: https:// part):

    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
    

    In this case you should use whether ssl module instead of socket, or just use requests since it is a simpler option.