Example of Simple Web Server Using Python

I may got the question wrong on my previous post.

So, here is how to make a simple web server using python in ubuntu.

  1. Open up your ubuntu terminal and create the 'index.html' file like this:
    <html>
    <head><title>python.my sample</title></head>
    <body>
    <h1>python.my sample</h1>
    <p>This is the simple html sample. Got it?</p>
    <p>Visit <a href="http://coderstalk.blogspot.com">Coder's Talk blog</a></p>
    </body>
    </html>


  2. In the same directory, create the python server and name it as 'pyserver.py' and enter the content like this:
    import SimpleHTTPServer
    import SocketServer

    theport = 1234
    Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    pywebserver = SocketServer.TCPServer(("", theport), Handler)

    print "Python based web server. Serving at port", theport
    pywebserver.serve_forever()


  3. Run the python code using this command:
    $ python pyserver.py


  4. Open up your web browser and go to http://localhost:1234 and see your python web server running.

That's all... Have fun coding

Comments

王同 said…
You may try this:

python -m SimpleHTTPServer 1234
ApOgEE said…
I've done that, see the screenshot. Thanks for commenting my blog.

Popular posts from this blog

How to Create Hyperlink on Blogger Post

How to Add a Sudo User on AlmaLinux 9.2 (Turquoise Kodkod): A Step-by-Step Guide

How to Show and Hide Text in Blog Post