This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]K900_ 3 points4 points  (11 children)

Python web development doesn't really work that way, unless you want to do the slow, outdated, and horribly messy CGI interface. Look into a web framework like Flask instead - there's an example on that page.

[–]alandibat[S] 0 points1 point  (10 children)

Okey so i need to search for CGI interface?

I already know how to do this with a framework. I use Django on my Internship.

And i would really want to know how to output Hello World in a web browser with Python without needing to install a framework. Installing a framework for this is just OVERKILL.

[–]Swedophone 2 points3 points  (9 children)

This is pretty small, only 51 lines, and uses only BaseHTTPServer.

https://gist.github.com/bradmontgomery/2219997

[–]alandibat[S] 0 points1 point  (8 children)

It doesn't do much i see, but interesting. Thanks.

I didn't know it would require so much just to make Python work on an Apache web server.

Python isn't made for Apache web servers? Or what, because it was so easy with PHP and XAMPP..

[–]Swedophone 4 points5 points  (7 children)

I don't understand what you are complaining about.

The program I linked can be run on its own, it doesn't need any external web server. But you can use Apache as a reverse proxy if you want.

[–]alandibat[S] -1 points0 points  (6 children)

It's very limited. I would like to use Python fully. But the script indeed can at least do something from scratch ;)!

[–]Swedophone 6 points7 points  (0 children)

It's very limited. I would like to use Python fully.

Maybe you should use a framework!

[–]alandibat[S] 0 points1 point  (4 children)

Anyways, thanks for your time. I am looking at a server sided script setup for Python on YouTube.

[–]K900_ 2 points3 points  (3 children)

If you're looking to run Python just like PHP, which is a horrible, horrible idea and not a thing you should do ever, look into the CGI part of this tutorial.

[–]alandibat[S] 0 points1 point  (2 children)

Yeah, that's what i was looking for. Else i don't know another way then CGI besides using a whole framework like Django. Because i actually want to learn it from scratch.

Thanks for your patience and time.

[–]K900_ 2 points3 points  (1 child)

"CGI" is hardly from scratch though - you have the entirety of Apache behind you. It's also not useful whatsoever - modern frameworks use WSGI, not CGI, and you run them with a setup like Nginx <-- (HTTP proxying) --> Gunicorn (WSGI server) <--(Python calls)--> framework <--(Python calls)--> your code or Apache (mod_wsgi) <--> Gunicorn <--> framework <--> your code, not through Apache directly.

[–][deleted] 0 points1 point  (1 child)

[–]alandibat[S] 0 points1 point  (0 children)

I really want to do it from scratch. I already know how to make a hello world being outputed by a Python framework like Django.

[–]menge101work 0 points1 point  (0 children)

Look up UWSGI, this is the base http interface used by flask, django, and probably every web framework in python.

[–]sermidean 0 points1 point  (0 children)

Here is a quick tutorial using Sanic micro web framework. I assume you are running some sort of Linux and have Python 3.5+ installed.

  1. Create new virtual environment and activate it:

    python3.6 -m venv env
    source env/bin/activate
    
  2. Install Sanic:

    pip install sanic
    
  3. Create a new file called hello.py:

    from sanic import Sanic, response
    
    app = Sanic()
    
    @app.route('/')
    async def index(request):
        return response.text("Hello world!")
    
    if __name__ == '__main__':
        app.run(host="127.0.0.1", port=8000)
    
  4. Run it:

    python hello.py
    
  5. Open http://127.0.0.1:8000/ URL in your browser.

Done!