InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Python

    Python strings

    • Python string quick start tutorial
    • Python string split examples
    • Python string lowercase and uppercase
    • Python string contains check
    • int to string
    • string to int

    Python List (Array)

    • list basics
    • list append, prepend
    • list remove an item
    • merge lists
    • slice of list
    • array clone
    • initialize list with same value
    • value in list check

    Python Dictionary

    • dictionary basics
    • Python iterate dictionary
    • dictionary get key with default
    • sort dictionary by values

    Python control flow

    • if-elif-else
    • for loop

    Python Regex

    • re search() vs match()
    • re.match()
    • re.search()
    • re.sub()

    Python Cookbook

    • Command line - run python webserver
    • How to find python package's file location
    • Python etl petl - read table from csv file
    • Python file read write examples

    Python built-in functions

    • filter
    • filter vs ifilter
    • itertools ifilter
    • itertools imap
    • len
    • map
    • print
    • range
    • rstrip
    • type
    • xrange
     
    • Home
    • > Tutorials
    • > Python

    Command line – run python webserver

    By admin on Sep 26, 2015

    Sometime we need to run a quick static webserver which can serve various files in a directory. This can be pretty handy to serve html and js content from a web server for html/javascript development and debugging purpose. This will work if no server side rendering is required. We are using Mac for the purpose of this tutorial, but it should work on Linux and windows also.

    Here are quick steps to run a web server on command line:

    $ mkdir mywebdir
    $ cd mywebdir
    $ echo "Hello world..." > hello.txt
    $ echo "Hello world2..." > hello2.txt
    $ python -m SimpleHTTPServer
    Serving HTTP on 0.0.0.0 port 8000 ...
    

    If you visit http://localhost:8000/ then you will see this page:
    python-sample-web-server-file-list

    You can also visit http://localhost:8000/hello.txt and see the following page:
    python-sample-web-server-one-file

    You should also see the following log entries in shell:

    1.0.0.127.in-addr.arpa - - [26/Sep/2015 21:31:13] "GET / HTTP/1.1" 200 -
    1.0.0.127.in-addr.arpa - - [26/Sep/2015 21:31:22] "GET /hello.txt HTTP/1.1" 200 -
    

    Changing default port

    To change the port to a custom port (say 5678) run the following:

    $ python -m SimpleHTTPServer 5678
    Serving HTTP on 0.0.0.0 port 5678
    

    Looking into the code

    In case you want to find the file location of the code for module SimpleHTTPServer, you can run the following:

    $ python -c "import SimpleHTTPServer; print SimpleHTTPServer.__file__"
    

    The above outcome for a mac machine. That mean module code can be viewed in following file:

    /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SimpleHTTPServer.py
    

    Suggested posts:

    1. migrate multiple mysql databases using mysqldump on Linux
    2. How to use pm2 to manage node.js application in production
    3. Mongo – cli quick start guide
    4. How to use phantomjs to create site/url snapshot thumbnail
    5. How to make best use of Alexa
    6. PhantomJS – find all fonts-families on a web page
    7. Memcache – how to dump all keys and values on command line
    8. Embed youtube video with javascript on-click lazy loading approach
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Linux, Linux/Unix Command Line, Mac, Python, Python cookbook, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress