Forums

Can I use websockets ?

Hi , can I use websockets in pythonanywhere as a server side ?

You can, but it's experimental and command-line only at the moment -- there's no web UI. If you're interested in trying that out, drop us a line at support@pythonanywhere.com -- it would also be useful to know which web framework you're planning to use.

Hi Giles, you mean , I can not run it forever ? (I'm using flask framework )

It just means that the feature is still in development. We might make changes to the pricing eventually.

Oh , ok . I want to use it just for a simple multiplayer game , send data to server and server send the data to all , something like this

Here's a help page for that: https://help.pythonanywhere.com/pages/ASGICommandLine/

Thanks for your fast reply :D I will check this out. Just I have a question , can I use websocket library in pythonanywhere and pass my site url to it to make a server on my site ? (I think this question has the same answer , if it is , sorry)

I read this article which you sent , but I have a multiplayer game which run on my computer , and I want to make a server on pythonanywhere which get informations such as my player position and ect. It updates information every 1 second

Yes, you can use websockets. Like we mentioned, the feature is still in beta, but I suggest just trying it out.

Thanks , I want to try it on my another account

Hi again , I use flask socket io on my pythonanwhere account , and try to connect to it from my computer , but it does not work !!

Here is server side which run on host:

#Server side which run on pythonanywhere
from flask import Flask
from flask_socketio import SocketIO

app = Flask("test")
socket = SocketIO(app,cors_allowed_origins="*")

@app.route("/")
def home():
    return "Hello , this is a simple server side !!"

@socket.on("message")
def message(data):
    socket.emit("message",f"Hello {data} , I am in the server side !!")

if __name__ == "__main__":
    socket.run(app,host="https://my_user_name.pythonanywhere.com",port=9090)

And client side which run on my computer:

from socketio import Client

socket = Client()
socket.connect("https://my_user_name.pythonanywhere.com/")

print("Connected to the server ...")

name = input("Your name : ")

socket.emit("message",name)

@socket.on("message")
def message(data):
    print(data)

socket.wait()

It works fine on my local computer (localhost) , but it does not work on pythonanywhere host , I can not connect to the server . Appereciate for any help

Let us know how it works for you.

As I said , I test these codes , and their work fine on localhost (when the server code run on http://127.0.0.1:9090/) , but when I put my server code on pythonanywhere and try to connect to it by my client code which run on my computer , it does not work . I get an error in client side which say :

websockets.exceptions.invalid url: https://my_user_name.pythonanywhere.com is not a valid url : scheme isn't ws or wss

You appear to be trying to use an https:// URL instead on wss:// or ws:// for websockets.

If I get it correctly , I should use (ws://my_user_name.pythonanywhere.com or wss://my_user_name.pythonanywhere.com) instead of (https://my_user_name.pythonanywhere.com) ? If yes , I should change the url in both server and client code ?

I think it would only need to change on the client side.

I am very sorry , I made mistake , I said wrong exception . The exception which I get is this : raise exceptions.ConnectionError(socketio.exceptions.ConnectionError: One or more namespaces failed to connect

I do not know why this happen :(

This is whole of the error : Traceback (most recent call last): File "c:\Users\User1\Desktop\test\client.py", line 4, in <module> socket.connect("https://singlestar.pythonanywhere.com") File "C:\Users\User1\AppData\Local\Programs\Python\Python312\Lib\site-packages\socketio\client.py", line 168, in connect raise exceptions.ConnectionError( socketio.exceptions.ConnectionError: One or more namespaces failed to connect

Your PythonAnywhere web app is not an ASGI application. Please follow the help page above to set up an ASGI app.

So , Can I use flask for that ?

No, Flask is WSGI-based and synchronous.

There is no way ? , Can I use this method ? Flask wsgi to Asgi

Because these above code which I sent work fine on localhost

I tested these codes and their work fine as I said , client can connect to the server which run on local host , sorry but , is there any problem with pythonanywhere websocket supporting ? Can you please guide me in this situation ?