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

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Modules In Python / How to use SimpleHTTPServer

How to use SimpleHTTPServer

Author: PFB Staff Writer
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.

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

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Modules In Python, simplehttpserver Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS Uncategorized urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Convert String to List in Python
  • Convert String to Set in Python
  • Largest Element in a List in Python
  • Insert New Column Into a Dataframe in Python
  • List of Dictionaries to Dataframe in Python

Copyright © 2012–2022 · PythonForBeginners.com

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