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

Asked

Viewed 1,817 times

1

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

But if I wanted to write small codes just for testing, there would be some way to do this in python without using web frameworks?

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

echo "Hello, World"

And then turn on the command line:

php -S localhost:8000

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

You can do it in Python?

  • 1

    The title question to have a little bit to do with the explanation of the question. Or there are two separate questions there. The answer to the title is: yes, it can. The last question is whether Python has a built-in server? This is really what you want to know?

  • More or less @bigown. I want to know if you have a hello_world.py and run it through the command line on a server (python itself) and appear there in a localhost of life the result (in the browser). PHP type, check?

  • How do you do it then, @bigown? In Python you can "write something to print in the browser"?

  • I don’t have much knowledge but I know that directly does not have, need extra tool. I’m reading about it now, maybe it’s what you want: http://waitress.readthedocs.org/en/latest/

  • In terms of web tools, what I can best recommend, what I’ve been experiencing, are languages, Apis, and frameworks, like HTML5; PHP; CSS3; MYSQL; NOSQL (ex.: MONGODB); NODE.JS; ANGULAR.JS; RUBY ON RAILS; and SYCL, by degree of ascending difficulty. If you are interested in computer graphics, you can also choose tools such as WEBGL. Of all these tools, what comes closer to its purpose at the level of programming paradigm, and software architecture is perhaps NODE, the ANGULAR, and the RUBY ON RAILS. Once again, it’s useless to try to reinvent

  • Why not use flask or Django? By the way, take a look at the CGI library.

  • As stated in the question itself: I want something simple

  • @Pablopalaces really, the "I want something simple" can fit very well with the use of the Flask.

Show 3 more comments

2 answers

7


First, Python is a completely different language from PHP. Although the PHP documentation says that this language is general purpose, it is undeniable that it is highly related to developing 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, developing web applications done purely with Python is a quite complex task (as opposed to its simple).

Having made the introduction, my answer to your question is nay, 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 scripts in Python.

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

  1. Create a directory to serve your files.
  2. Inside this directory, create a subdirectory called cgi-bin
  3. 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!""")
    
  4. Change file permissions to be executable

  5. Start the server with the option to serve CGI scripts:

    python -m http.server --cgi 8000
    
  6. Visit localhost:8000/cgi-bin/hello.py and you’re done.

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

Note that with this same approach, you can also run scripts in PHP, just by changing the shebang.

Another possibility is, instead of creating a root directory to run the Python server, use Apache’s own cgi-bin folder (if you’re using it and it’s configured to handle CGI). That might make things a little simpler. As said in the commentary, you can use the cgi and cgitb library, which can simplify your life a little.

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

  1. Install the flask pip install flask
  2. 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()
    
  3. Run the script python hello.py

  4. Visit localhost:5000 and ready.

That way you didn’t have to set up a directory structure, didn’t have to deal with shebangs, didn’t have to change file permissions and didn’t have to deal with HTTP headers.

Okay, it’s 10 lines of Python, with Developer and everything, but try testing a POST using only Python and CGI and then testing a POST with Flask. I say this because I believe you will not test only "echo hello Worlds"...

-1

Yes, you can use the module SimpleHTTPServer.

You can rotate 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: https://docs.python.org/2/library/simplehttpserver.html

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

spin:

python -m http.server 8000

or add inside 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 in:

https://docs.python.org/3.4/library/http.server.html

  • Okay, and how are you going to run scripts in python with this?

  • writes about do_GET

  • Really, if you try to run a script python this way, it will download the file or it will display the content of the script. Actually, this is a way for python to create a server to run only HTML and the like.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.