0

I'm currently working on a Socket.IO implementation using Python with socketio and eventlet for my server and socketio for my client. when i use the http it works fine client is able to connect to server and everything happens as intented , but if i try to connect the client using the https it is not able to connect it gives the error as code 400, message Bad request version.

Here's a simplified version of my server (server.py):

server.py:

import socketio
import eventlet
from threading import Thread
import redis
import time

sio = socketio.Server()
app = socketio.WSGIApp(sio)

redis_conn = None


def initialize_redis_conn():
    global redis_conn
    if redis_conn is None:
        redis_conn = redis.StrictRedis(host='localhost', port=6379, decode_responses=True)
        print("redis connection init")


def insert_update_sid(sid, unique_id):
    """
    Inserts or updates the sid and uniqueId of the client into the Redis cache.
    """
    initialize_redis_conn()
    key = f"DATA_KEY_{unique_id}"
    expiry_in_seconds = 1800
    redis_conn.setex(key, expiry_in_seconds, sid)
    print("Data inserted into Redis successfully")


def get_sid_by_unique_id(unique_id):
    """
    Returns the sessionId (sid) of a client using the uniqueId.
    """
    initialize_redis_conn()
    print("fetching sid")
    key = f"DATA_KEY_{unique_id}"
    sid = redis_conn.get(key)
    if sid:
        print(f"Got sid {sid} using uid {unique_id}")
        return sid
    else:
        return None


def run_sqs():
    data = {
        "communicationStatus": "OKAY",
        "status": "Complete"
    }
    unique_id = "123789"
    if unique_id:
        print("inside sqs impl is : ", unique_id)
        sid = get_sid_by_unique_id(unique_id=unique_id)
        if sid:
            print("calling send response to client")
            send_response_to_client(sid, data)
            print(
                f"Response has been sent to the client with sessionId: {sid} and uniqueId : {unique_id}")
        else:
            print(f"Client not found for the given uniqueId: {unique_id}")


def send_response_to_client(sid, resp):
    """
    this func sends the response to the client
    """

    sio.emit('response_from_server', f'response : {resp}')
    print("data sent to client ")


@sio.event
def connect(sid, environ):

    print(f"client {sid} connected")
    print("sid type is: ", type(sid))
    token = environ.get('HTTP_TOKEN')
    sio.emit("acknowledge", "ack recevied from server", room=sid)
    if token:
        auth_status_code = 200
        if auth_status_code == 200:
            unique_id = "123789"
            # insert the sid and uniqueId into redis cache 
            insert_update_sid(sid, unique_id)


@sio.event
def disconnect(sid):
    """
    this is a disconnect event which disconnects the client from server
    """
    print(f"client {sid} disconnected")

def start_server():
   eventlet.spawn(run_sqs)
   eventlet.spawn(eventlet.wsgi.server(eventlet.listen(('0.0.0.0', 5000)), app))


if __name__ == '__main__':
    start_server()

And my client (client.py):

client.py:


import requests
import socketio

PORT = "5000"

IP = "localhost"

sio = socketio.Client()


@sio.event
def connect():
    print("connection established to server connect event")


@sio.event
def disconnect():
    print("client disconnected from server")


@sio.event
def server_response(response):
    print("response from server recived")
    print(f"response received from server: {response}")


@sio.event
def acknowledge(ack):
    print(ack)


if __name__ == '__main__':
    data = {}
    stage = "dev"
    token = get_token(stage)
    token = "Radom_token"
    if token:
        print("Token has been generated.")

        url = f"http://{IP}:{PORT}"

        sio.connect(url, transports=['websocket', 'polling'], headers={'token': token})
        sio.wait()

    else:
        print("Token has not been generated ")

i am actually running this server.py in aws ec2 and i am connecting it with ip using http it worked but when tried the same with https it is giving error :

code 400, message Bad request version ('À\\x13À')
"\x16\x03\x01\x01\x4\x01\x00\x01\x00\x03\x03n}\x99ºSíÕêwí\x0bà¶\x1a>ìx±¦\x18g\x09¡TÏ\x16Hª¦oŤ Z^Ðæw\x1a\x8fЫä³\x8f¬}Dr!]{\x09\x8d¼\x8cã\x90IUȳ\x84Fª\00.\x13\x01\x13\x02\x13\x03\x13\x04\x13\x05À+À/À,À0̨̩À\x09À\x13À" 400 -
2
  • Have you configure your web server with TLS? The garbage that you are getting suggests that the server expects plaintext connections, not TLS. Commented Mar 12, 2024 at 12:30
  • My bad i did not know about this and haven't configured it yet , and thank you for the response Miguel Commented Mar 12, 2024 at 12:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.