skip to Main Content

So I have a Python web socket server that runs on my server’s port 8080. It is a Ubuntu 20.04 server that uses ufw (Uncomplicated Firewall) for its firewall.

I typed:
sudo ufw allow 8080 to open the port 8080 on my system, and then started the python server. Now the thing is that my Python websocket server has been started but when I go to https://portchecker.co and type my server IP + port, it says that port 8080 is closed, even though the server is started and the port is opened via ufw.

To test this more, I tried starting an http server with port 80 opened and checked it on https://portchecker.co and voila, it says that port 80 is opened.

This is my Python Websocket server’s code:

import asyncio
import websockets
import random
import string
import subprocess
import os
import logging
import ssl
import speech_recognition as sr
r = sr.Recognizer()

logging.basicConfig()

ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)

ssl_cert = "/etc/letsencrypt/live/edlivon.com/fullchain.pem"
ssl_key = "/etc/letsencrypt/live/edlivon.com/privkey.pem"

ssl_context.load_cert_chain(ssl_cert, keyfile=ssl_key)


async def write_audio(websocket, path):
    audio_data = await websocket.recv()
    folder = "audios"
    filename = ''.join(random.choices(string.ascii_lowercase, k=12))
    file_format = ".wav"
    output_path = folder + "/" + filename + file_format

    with open(folder + "/" + filename + ".webm", "wb") as f:
        f.write(audio_data)

    subprocess.run(["ffmpeg", "-i", folder + "/" + filename + ".webm",
                   "-acodec", "pcm_s16le", "-ar", "48000", output_path])

    with sr.AudioFile(output_path) as source:
        audio = r.record(source)

    result = r.recognize_google(audio, language="en-US")

    os.remove(folder + "/" + filename + ".webm")
    os.remove(output_path)

    response = result
    await websocket.send(response)


async def server(websocket, path):
    await write_audio(websocket, path)


start_server = websockets.serve(server, "localhost", 8080, ssl=ssl_context, origins="*")


asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

To start the websocket server, I simply run python3 app.py and I have all the dependencies of this script installed and working properly, running the script produces no error. Can anyone help me out with fixing this?

2

Answers


  1. Chosen as BEST ANSWER

    A kind human being just told me that I should replace localhost with 0.0.0.0 and suddenly, it works.


  2. maybe they have a firewall between you and the internet

    you could test by opening a separate session in the server and trying to connect to your script locally

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search