Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Python server setup

Tags:

python

http

I am trying to learn python (coming from PHP), and want to set up the simplest web server so I can start coding.

I found the integrated HTTP server, so I figured it should be the easiest way.

root@ubuntu:/var/py# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

The webserver is working, accessing http://test.dev:8000/test.py (thanks /etc/hosts) works - but shows me the contents of the file ( print('Hello world!'); ), without interpreting it.

How to properly set up the server / interpretor ?

like image 558
Vlad Preda Avatar asked Jun 08 '13 11:06

Vlad Preda


People also ask

Can you make a server with Python?

A webserver in Python can be setup in two ways. Python supports a webserver out of the box. You can start a web server with a one liner. But you can also create a custom web server which has unique functionality.

What is simple HTTP server in Python?

The SimpleHTTPServer module is a Python module that enables a developer to lay the foundation for developing a web server. However, as sysadmins, we can use the module to serve files from a directory. The module loads and serves any files within the directory on port 8000 by default.

Does Python have a built in web server?

Python standard library comes with a in-built webserver which can be invoked for simple web client server communication. The port number can be assigned programmatically and the web server is accessed through this port.

How to setup a web server in Python?

A webserver in Python can be setup in two ways. Python supports a webserver out of the box. You can start a web server with a one liner. But you can also create a custom web server which has unique functionality. In this article you’ll learn how to do that. The web server in this example can be accessed on your local network only.

Does Python have a web server?

Python supports a webserver out of the box. You can start a web server with a one liner. But you can also create a custom web server which has unique functionality. In this article you’ll learn how to do that. The web server in this example can be accessed on your local network only.

What do I need to run Python code on a server?

The http.server (or SimpleHTTPServer for Python 2) module in Python is handy, but it’s only a static file server; it can’t run code written in Python, PHP, or JavaScript. You’ll need something extra to deal with them, and just what you’ll need depends on the server-side language you’re using.

What is the best way to access a Python Server?

Python simply allow you to access your desktop/server contents via a web browser. This method is not recommended for production use. Use it within trusted home networks. Also, don't forget to set proper permissions to avoid data loss and misuse.


2 Answers

Python is for writing programs in general, not only web-sites.

SimpleHTTPServer is really just a trivial HTTP server, it serves files. It doesn't try to interpret code.

If you want to do web-development with Python, you should be using a web framework. web.py is a good choice, you can check its tutorial. Another option is Django, which also has a brilliant tutorial.

Both of them have simple development servers built in, so you'll be able to start experimenting easily.

like image 199
kirelagin Avatar answered Oct 17 '22 03:10

kirelagin


If you are just starting out with python I would recommend you start with scripts that can be run from the console using python interpreter. (eg: python run1.py)

Once you have mastered the basics, you can move onto web programming. (I am guessing that you want to try web programming since you mention a web server.) In this case, you have multiple options (all of which work with Apache):

  • Django framework : Really good framework, has a built-in webserver and has fantastic documentation (http://www.djangobook.com/en/2.0/index.html). You need to know python basics first
  • WSGI: https://code.google.com/p/modwsgi/ : Apache module for running python code
like image 2
krthkr Avatar answered Oct 17 '22 03:10

krthkr