• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

How to create Simple Python HTTP Server for 2.x and 3.x Version?

Aniruddha Chaudhari/30834/12
CodePython

Python is really incredible with its simplicity and capability.

In this tutorial, we see how simple one-line command can turn your system into a Python HTTP server machine.

Just only one-line command. That makes Python more interesting.

It does not require any external library. All the required modules come preinstalled with Python.

Install Python on your system and you are ready.

Command to Create Python HTTP Server

There are different commands for different Python versions.

Your system can have multiple Python versions installed on your system. You can always check the Python version installed on your system before starting the server.

Run the following command to create a Python HTTP server on the local machine.

python -m SimpleHTTPServer #default port 8080

Note: This command supports on Python 2.x version. It does not support for python 3.x.

If you run the same command on python 3.x, you can get an error as

python: No module named SimpleHTTPServer

Use the following command to create HTTP server on your workstation with Python 3.x version.

python -m http.server

Command to Run Simple Python HTTP Server

Port number can be anything and you can see it on command console after running this simple python script.

Note: If 8080 port is not used by any of the processes on your system, it will be the default port number.

Now your server is ready to access.

Accessing Python Server

To test it on, open browser. Type in the address bar as “localhost:8080” and run.

You can see all the directory files accessible through the browser. Other addresses through which you can access files is 127.0.0.1:8080.

You can also use your own computer private IP address. You can find your computer IP address using “ipconfig” command in command prompt.

When you access the python HTTP server through a browser, it sends the request and response in the form of HTML. You can see all the request and responses on python console where you run the server script.

 Console for Python HTTP Server Request Response
You can access server files through other devices as well. For that, it is necessary to connect the device to the same network on which python server is running.

Use cases for Python HTTP Server

As FTP Server:

How to create an FTP server using Python for file transfer?

This trick is very much helpful if you want to transfer data from computer to other devices.

Personally, I use it to transfer movies or some other big files from my computer to Smartphone.

Python FTP Server

You can see all the directory listing from the folder where we have created Python HTTP server.

I prefer to transfer the files this way. It does not require any USB cable, LAN cable or any storage device to transfer files. As it uses private IP addresses to connect server and other devices, it is necessary to have all devices connected to the same network.

As HTTP Server:

You can use the Python HTTP Server for web development purpose as well. You no need to install any third-party software. Just create Python HTTP Server and save all the webpages into the folder where you have created python server.

Suppose you have this HTML page.

<html>
<body>
<strong>Welcome to CSEStack</strong>


We live and breathe the code...
</body>
</html>

Save it in python server folder as “index.html”.

Now hit the server from a web browser. You can see the HTML index page output.

Python server as HTTP Server

Once you have done with your use, you can stop the server by clicking “ctrl+c”. It interrupts the server and forces to stop.

If you want to master Python, checkout complete Python tutorial. We are continuing with an amazing non-exhaustive list for Python tricks and scripting. Do check this space for more updates.

Happy Pythoning!

Python Interview Questions eBook

Pythonserver
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Shardul Silswal
    January 25, 2018 at 11:49 pm

    Hey! really great info. Loved it.
    Can you please brief on how to use this to transfer data to mobile?

    • Reply
      Aniruddha Chaudhari
      January 26, 2018 at 7:12 am

      Thank!

      If your mobile and computer are connected to the same network, you can open “localhost:8080” in your mobile browser.

      After that, you can browse through all the shared folders and contents in the folders.

      • Reply
        Shardul Silswal
        January 26, 2018 at 2:15 pm

        Thanks but I tried opening “localhost:8000” in my mobile browser and it didn’t work. Is it because I am connected to my hostel wifi on both my devices? And this might not work for a public wifi?

        • Reply
          Aniruddha Chaudhari
          January 26, 2018 at 3:29 pm

          If it is not working, try this. It works…

          Get the IP address of your system. You can get the IP address by running command

          ipconfig (on windows)
          ifconfig (on Unix)

          Open mobile browser and run URL

          ip_address:port_used_by_Python_for_http_server

          • Shardul Silswal
            January 27, 2018 at 1:57 am

            It worked.
            Thanks a lot!

          • Aniruddha Chaudhari
            January 27, 2018 at 10:13 am

            Not a problem 🙂 Happy Programming!

  • Reply
    Egxie
    August 19, 2019 at 2:13 am

    Python is the best language for automation. Nice one. Keep it up…

    • Reply
      Aniruddha Chaudhari
      August 19, 2019 at 9:27 am

      Indeed. Thanks, Egxie!

  • Reply
    vamsi
    February 4, 2020 at 7:17 am

    http.server, it can handle only HTML files

    or

    it can handle both HTML and python files?

    • Reply
      Aniruddha Chaudhari
      February 4, 2020 at 5:28 pm

      It can handle both HTML and Python.

      • Reply
        anmol
        June 24, 2021 at 6:50 pm

        can it handle PHP files?

        • Reply
          Aniruddha Chaudhari
          June 24, 2021 at 11:11 pm

          The steps mentioned in this tutorial, develop a local host environment. You can execute any web application under it, so the PHP.

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard