Forums

Using PythonAnywhere as a game server

I've created an client/server card game, but it only works on my local network. I've been looking for a way to play with my family who are in a different country, and I thought I could use PythonAnywhere to run the server code (although I expect I'll have to modify it heavily). Obviously the server needs to be able to accept packages, but since it would probably have a hard time sending packets back to my computer (I'm in a college dorm, so I don't have access to the router to enable port forwarding or anything like that) it'd be best if the clients could just check back with the server to get the game states. Is this possible with PythonAnywhere, or is there an easier way of going about it? If so, what's a good example/resource to get started?

Yes (with caveats).

The way I would do it, would be with a Python backend web framework like Flask or Bottle.

I would make the clients send http requests http://yoursite.pythonanywhere.com/gamestate/x

And get the server to respond accordingly. On the client side, I'd use requests or urllib.

This will be far easier to code, and maintain than passing packets around directly.

what rcs1000 said -- it depends what kind of communications go between the client + server. If it's straight HTTP, and you can rewrite your app so that it implements the WSGI protocol (eg, using flask or django), then you're fine. If you're using websockets, or you need raw socket connections, then you won't be able to do that here...

So I'm a little new to networking, if that wasn't obvious. Does anyone have a good beginner tutorial or maybe some documentation I can look at to get me started?

Have a look at the tutorials for Django and Flask. Both are very good.

For what you're doing flask would be the easiest way. It's simple, easy to debug, and you can get your back-end working in a matter of minutes.

See: http://flask.pocoo.org/

Ok, so I have some proof-of-concept code running, it just uses the GET and POST html methods. The function look kinda like this:

@app.route('/game', methods=['GET','POST'])
def game_config():
    if request.method == 'POST':
        #set things
    return #some string

It would be super helpful if the method could return a dictionary or a couple of lists or something, but when I do that it breaks. Is there a way to return something that isn't a string, or is that my only option? My client code just does a post request with some data to mywebsite.pythonanywhere.com/game that the game_config function changes to a string, and when I need what's been posted, I just call a get request on the same site and I get some data in the form of a string.

This one's easy - you need to use pickle (specifically pickle.dumps) to convert your dictionary to a string, which you can then return.

On the client side you use pickle.loads to convert the string back into a dictionary (or whatever).

You could also use json (Python docs here and Flask also has support) if your app needs to work with non-Python clients.

Why would anyone use a language other than Python?

Cause it's hard (not impossible) to run Python in a browser?

If a platform doesn't support Python, then that platform is not worth using.