0

I'm having problems with a simple socket connection to an Heroku app.

This is my server:

import socket
import os
import time 
import sys

server = socket.socket()
port = int(os.environ.get("PORT", 12344))
host = "0.0.0.0"
server.bind((host, port))
print(f"###### SERVER RUNNING ON PORT {port} ({host}) ######")
server.listen()

while True:

    s, addr = server.accept()
    print("Recived request from:", addr)
    
    print(addr, " sent: ", repr(s.recv(1024)))
    
    print("Answering to:", addr)

    s.send("Hello, world! (from server)".encode())
    
    print("Answered to:", addr)

    s.close()

It builds and run perfectly on Heroku(it receives also socket connection, not from me...by at this time, I don't care much about it)

This is my client:

import socket
import sys

HOST = 'app_name.herokuapp.com/'  # The server's hostname or IP address
PORT = int(sys.argv[1])            # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    print("connecting to " + HOST +":"+str(PORT))
    s.connect((HOST, PORT))
    print(s)
    s.sendall("HI!!!".encode())
    data = s.recv(1024)

print('Received', repr(data))

Running the client, after a while it returns:

  File "./client.py", line 14, in <module>
    s.connect((HOST, PORT))
TimeoutError: [Errno 110] Connection timed out

I don't know how to connect to it...am I missing something?

1 Answers1

0

The problem is linked to the fact that Heroku app allow only http(s) connections. I honesty didn't found any Heroku docs that conferm it but the Daniel Chin' answer to this question make sense to me.

However, move the server.py to AWS worked for me (EC2 with t2.micro is free for the first year!!)