Is there any way to write a web code without a python framework?

1

I find python a very interesting language. And as a web developer, I'd like to use it for this purpose. I know there are good frameworks like Flask and Django .

But if I wanted to write small test-only code, would there be any way to do it in python without using web frameworks?

For example, in PHP I can create a simple script:

echo "Hello, World"

And then run on the command line:

php -S localhost:8000

This would create a simple server and run my "hello world".

Can you do this in Python ?

    
asked by anonymous 12.09.2015 / 18:51

2 answers

6

First, Python is a totally different language from PHP. Although the PHP documentation says that this language is general purpose, it is undeniable that it is highly related to the development of scripts to be run on a web server.

In contrast, Python is a language that, among many other things, can also be used for web application development. However, due to this plurality of applications, web application development done purely with Python is a rather complex task (as opposed to its simple ).

Made the introduction, my answer to your question is not , it is not possible. The tovmeod response, as far as I know, only allows you to start a web server to manage static files and not to run Python scripts.

Following his approach, and as I suggested in the comments, you can run Python scripts as if it were a CGI script. So to make your simple "hello world" in Python you need:

  • Create a directory to serve your files.
  • Within this directory, create a subdirectory called cgi-bin
  • There, create a python module called hello.py with the following content

    #!/usr/bin/env python
    print("""Content-type: text/html
    
    Hello world from Python!""")
    
  • Change the permissions of the file to be executable

  • Start the server with the option to serve CGI scripts:

    python -m http.server --cgi 8000
    
  • Visit localhost: 8000 / cgi-bin / hello.py and that's it.

  • Note that in this approach you need to define the Content-type header, otherwise the browser may not understand and you will download the script instead of running it. The same goes for the shebang in the first line.

    Note that with this same approach, you can also run PHP scripts by simply switching the shebang.

    Another possibility is to use Apache's own cgi-bin folder instead of creating a root directory to run the Python server (if you are using it and it is configured to handle CGI). This can make things a bit simpler. As I said in the comment, you can use the cgi and cgitb library, which can somewhat uncomplicate your life.

    But anyway, my point is, you will hardly create a web application with Python using these approaches. Frameworks like Django and Flask are there to simplify development for the web. In your specific case, doing a hello world with Flask would be much simpler :

  • Install the flask pip install flask
  • Create the hello.py file anywhere with the following content:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello():
        return 'hello from flask'
    
    if __name__ == '__main__':
        app.run()
    
  • Run the script python hello.py

  • Visit localhost:5000 and that's it.
  • That way you did not need to set up a directory structure, did not have to deal with shebangs, did not need to change file permissions, and did not have to handle HTTP headers.

    Okay, there are 10 Python rows, with decorator and everything, but, try testing a POST using only Python and CGI and then test a POST with Flask. I say this because I believe you will not just test "echo hello worlds" ...

        
    20.09.2015 / 21:50
    -1

    Yes, you can use the SimpleHTTPServer module.

    You can run it simply:

    python -m SimpleHTTPServer 8000
    

    Or put this in your code:

    import SimpleHTTPServer
    import SocketServer
    
    PORT = 8000
    
    Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    
    print("serving at port", PORT)
    httpd.serve_forever()
    

    See: link

    If you are using Python 3, the module is now called http.server , the examples are similar:

    rode:

    python -m http.server 8000
    

    or add within your code:

    import http.server
    import socketserver
    
    PORT = 8000
    
    Handler = http.server.SimpleHTTPRequestHandler
    
    httpd = socketserver.TCPServer(("", PORT), Handler)
    
    print("serving at port", PORT)
    httpd.serve_forever()
    

    see more at:

    link

        
    13.09.2015 / 15:25