Socket connection with AWS sends ok but doesn't receive

I am trying to connect to an AWS instance using python socket connection. The AWS server is receiving the first handshake and tries to reply. On the Anvil part this message is not coming through resulting in a timeout.

I have tested the same connection from a local Python script and it is working fine. What could be the reason that the inward messages to Anvil are not coming through?

python
import anvil.server
from datetime import datetime
import socket
import time
import threading
import comfunct as CF
import shared as SH

socket variables

HOST = ‘xxxxx’ # The server’s hostname or IP address
PORT = xxx# The port used by the server
global encoding
encoding = ‘utf-8’
curr_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

@anvil.server.callable
def open_socket():
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
print(local_ip)
print(“CLIENT: ik open nu de verbinding”)
IPAddr = socket. gethostbyname(hostname)
curr_sock.connect((HOST, PORT))
global data
data = curr_sock.recv(4096)

while True:
    global data_con_code
    global msg
    global full_msg
    global mess_eval_1
    global mess_eval_2
    full_msg = ""

    new_msg = True
    msg = curr_sock.recv(4096)

    funct_dict = {
        "asa_mes": "CF.asa_mes",
        "asa_question": "CF.asa_question",
        "asa_questionyn": "CF.asa_questionyn",
        "internmes": "CF.internmes",
        "grenzen": "CF.grenzen",
        "startup": "CF.start_up",
        "next_exercises": "CF.add_exercises",
        "report": "CF.report",
        "close_ASA": "CF.close_student"
    }

    if new_msg:
        full_msg += msg.decode(encoding)

Perhaps the issue is that the Anvil server instance stops running after each server function call completes. See the “Global variables in Server Modules” heading here: https://anvil.works/docs/client/python/modules#global-variables

Thank you Tim!! Much appreciated!

1 Like