Setting Up a Python-powered Localhost

When designing/developing a website, normally you’ll setup a local server (known as a localhost partly because of the http://localhost:8000 URL used to access it) to test the code and configuration over an HTTP connection to ensure everything runs correctly. However, doing so can be quite hard. I’ve previously used XAMPP for a localhost, but that required me to download a huge installer or Zip archive (granted, there were a lot of content), run the services, then either copy my project to a special folder (either manually or through a script) or work out of that folder. After doing that for a day’s work, I decided against that option. Adobe Brackets has a Live Development feature that kinda duplicates that feature, but it currently requires Chrome. Isn’t there another, simpler way to setup a localhost to test my site?

Apparently there is, and is uses something I already have installed: Python!

I was reading an open issue on the Adobe Brackets GitHub repository asking for Live Development LAN support so he could test his site on Windows (he uses a Mac). Someone replied saying that this was not completely needed because Python can do this for you. The person said they uses this method themselves for testing (they gave an example of Apple Safari on an iPad), and provided the code for both Python 2 and 3.

While the live preview is nice for designing layout/style changes, I find it is usually much more practical, especially when working with Javascript, to create a static local web server and just refresh the browser to see changes …
That way it is possible to view the project from any browser on the local network, for example Mobile Safari on an iPad, rather being limited to Chrome on the same machine.

Here is a quick solution I use:

cd /path/to/project
Python 2.x python -m SimpleHTTPServer 8000
Python 3.x python -m http.server 8000

I tested this myself, and lo and behold, it worked! While it only broadcast on my laptop (there are obviously other things needed for it to work on our LAN), I was able to test my site against IE 11, Firefox, Chrome, and Opera using an HTTP connection! 😀

Because I wanted to help spread the word about this and help you use it yourself, I’ve written up this small Python 2 and 3 compatible script to automatically launch the localhost. Basically it relaunches the Python interpreter using the -m command, passing the proper module and port number. All you have to do is type in the port number (hint: only integers allowed) to use. I use port 717 just because I can. 😛 I have tested this on Python 2.7.3 and 3.3.3, and it all works. 🙂

Have fun testing your site everyone! 😀

-le717

Advertisements

5 thoughts on “Setting Up a Python-powered Localhost

Triangular Reactions

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s