• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Python Tutorial
  • Python Basics
  • Python Code Examples

 

You are here: Home / Modules In Python / How to use SimpleHTTPServer

How to use SimpleHTTPServer

Last Updated: August 27, 2020

Overview

In this post we will look at the built-in web server in Python.

What is it?

The SimpleHTTPServer module that comes with Python is a simple HTTP server that
provides standard GET and HEAD request handlers.

Why should I use it?

An advantage with the built-in HTTP server is that you don’t have to install
and configure anything. The only thing that you need, is to have Python installed.

That makes it perfect to use when you need a quick web server running and you
don’t want to mess with setting up apache.

You can use this to turn any directory in your system into your web server
directory.

How do I use it?

To start a HTTP server on port 8000 (which is the default port), simple type:

python -m SimpleHTTPServer [port]

This will now show the files and directories which are in the current working
directory.

You can also change the port to something else:

$ python -m SimpleHTTPServer 8080

How to share files and directories

In your terminal, cd into whichever directory you wish to have accessible via
browsers and HTTP.

cd /var/www/

$ python -m SimpleHTTPServer

After you hit enter, you should see the following message:

Serving HTTP on 0.0.0.0 port 8000 …

Open your favorite browser and put in any of the following addresses:

http://your_ip_address:8000
 
http://127.0.0.1:8000

If you don’t have an index.html file in the directory, then all files and
directories will be listed.

As long as the HTTP server is running, the terminal will update as data are
loaded from the Python web server.

You should see standard http logging information (GET and PUSH), 404 errors,
IP addresses, dates, times, and all that you would expect from a standard http
log as if you were tailing an apache access log file.

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial

Summary

In this post we showed how you with minimal effort can setup a web server to
serve content.

It’s a great way of serve the contents of the current directory from the command
line

While there are many web server software out there (apache, nginx), using Python
built-in HTTP server require no installation and configuration.

More Reading

http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python

osxdaily.com

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial

Filed Under: Modules In Python, simplehttpserver Date Originally Published: July 3, 2013

More Python Topics

API Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line crawler Data Types Development Dictionary Dictionary Data Structure In Python envoy Errorhandling Error Handling Exceptions Fabric Files fnmatch ftplib Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pil pip Python Python Code Snippets Python On The Web Python Strings Requests Scraping Scripts sh simplehttpserver System & OS urllib2 Web

Primary Sidebar

Get Our Free Guide To Learning Python

Menu

  • Python Basics
  • Code Examples
  • Loops
  • Functions
  • Strings
  • Python on the Web
  • Lists
  • Dictionaries
  • Python Modules
  • Python Glossary
  • Learn Python

Most Popular Content

  • Reading and Writing Files in Python
  • String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use Split in Python
  • How to use comments in Python
  • Python Syntax Basics

Recent Posts

  • Datacamp Review 2020
  • Most Common Python Interview Questions For 2020
  • Python 2 Vs Python 3 with Examples
  • How To Run Your Python Scripts
  • The 5 Best Python IDE’s and Code Editors for 2019

Python Courses

  • Datacamp: Intro To Python
  • 2021 Complete Python Bootcamp
  • Python Mega Course: Build 10 Real World Apps
  • Python Data Science Bootcamp
  • Complete Python Developer: Zero to Mastery

Copyright © 2012–2021 ยท PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us