Forums

Simple Server Example

From what I can gather I can't use raw TCPI/IP sockets in PythonAnyWhere. I would like to see an example of a server script I can run here in Pythonanywhere which would respond to requests sent from a web browser. A simple HTTP server running here in pyanywhere.

Could I run something like the SimpleHTTP server example here?

I have done quite a bit of Python for file, i/o and TCP sockets but I have not done much web stuff. Any step by step guides would be much appreciated.

Hi Eoin,

Our model for web apps involves using Python's WSGI protocol, which is what most Python web frameworks like django, flask and so on use.

You can configure a web app using one of these frameworks via the "Web" tab on your dashboard. Choose "manual configuration" if you want maximum flexibility (and then you can even write your own implementation of the wsgi protocol, although that's brain damage).

My colleague Giles wrote a guide to building a basic database-driven site with flask the other day, you might enjoy that.

Ok I found this code:

enter code here

from cgi import parse_qs, escape

def hello_world(environ, start_response):
    parameters = parse_qs(environ.get('QUERY_STRING', ''))
    if 'subject' in parameters:
        subject = escape(parameters['subject'][0])
    else:
        subject = 'World'
    start_response('200 OK', [('Content-Type', 'text/html')])
    return ['''Herro %(subject)s
    Hello %(subject)s!

''' % {'subject': subject}]

if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    srv = make_server('localhost', 8083, hello_world)
    srv.serve_forever()

How can I run this in python anywhere and communicate with it from my web browser? I already stored this file in an example called socket.py in my files here

Hi Eoin,

Head over to the "Web" tab and create a new web app. Choose whichever Python version you like, and "Manual configuration". That will take you to the web app configuration screen, and from there, find the link to your "WSGI file". this is where all the wsgi action happens. You'll find we include a simple "hello world" app that looks a lot like yours.

Then you can either import your code, or just edit the code in there. YOu don't need to do the "make_server/serve_forever" parts, we take care of that for you.

But, like I say, raw wsgi protocol is not a lot of fun to work with. Unless you're hacking about with it out of pure curiosity, I'd recommend using a framework like flask or django if you want to get things done. flask is pretty minimal and stays out of your way, django does more for you and has a bigger community/ecosystem probably. and there are others too...

Ok I am still lost.

I followed your steps, the Hello world example is quite pointless, it's just one line of code.

Where can I find the python file that runs when I enter http://eoin.pythonanywhere.com/ into my browser?

On my PC i have to run the WSGI python code and only then can I try in my browser http://localhost:8083/

I want to see and to be able to modify the webserver python script that is running.

By default I am getting this response without me having ran any scripts here:

Blockquote

Hello, World!

This is the default welcome page for a PythonAnywhere hosted web application.

Find out more about how to configure your own web application by visiting the web app setup page

The Python code that is running is the one that's linked from the "WSGI file" section of the "Web" tab. If you replace the contents of that file with your WSGI code, then reload the web app from the "Web" tab, then your code will run instead.

Hello there, I've been exploring the help for building a site web that executes my python scripts, but I'm having some trouble. I have already a web site running, I built it following a django tutorial, now I'd like my site to be able to interact with visitors (for example, uploading a text file with names and executing a search over it).

I tried simple stuff like printing 'hello world', I modified the "WSGI" file, but what it does is displaying on the console, so there is definitely something that I don't get. Could you please tell me if there is a turorial where I can find examples of scripts running on the site?

Thanks !!

If you want to execute things with a web interface, you'll need a web app on PythonAnywhere. You can then use that to execute your code. There are a number of tutorials on the help site about how to get started with web apps.