Monday 5 February 2018

A small HTTP server in Python

The Python http.server module can be run from the commandline with

python3 -m http.server

to provide a simple http server that listens on a configurable port and basically serves any file from the directory it is started from.

That is a good start but I had some additional requirements to serve up files from my raspberry Pi:

  • The server must be able to run as a demonized process, i.e, in the background
  • It must server a limited set of files
  • from a configurable directory and
  • be able to run as a less privileged user
Now of course I could have opted for a light weight http server like nginx or lighttpd, but I decided to write my own so I wouldn't have to install a boatload of additional packages.

The server is based on the daemon module a wrote about in a previous article and can be started from the commandline in much the same way as http.server:


It will run as the user that started it, will listen on port 8000 and start serving any file from the current directory. It will demonize itself as well.

It takes some additional options:

python3 -h

usage: [-h] [-p PID_FILE] [-l LOG_FILE] [-r ROOT_DIR]
                               [-n NAME] [-u USER] [-f] [-s] [-d]
                               [--bind ADDRESS] [-x] [-e EXT]

Example HTTP Server

positional arguments:
  port                  Specify alternate port [default: 8000]

optional arguments:
  -h, --help            show this help message and exit
  -p PID_FILE, --pid-file PID_FILE
  -l LOG_FILE, --log-file LOG_FILE
  -r ROOT_DIR, --root-dir ROOT_DIR
  -n NAME, --name NAME  name of the server used in log lines
  -u USER, --user USER  drop privileges of running server to those of user
  -f, --force           start a server even if pid file is present already
  -s, --stop            stop a running server
  -d, --debug           Run http server in foreground mode
  --bind ADDRESS, -b ADDRESS
                        Specify alternate bind address [default: all
  -x, --nodirlist       never show a directory listing
  -e EXT, --ext EXT     allowed file extensions (without .) May occur more
                        than once
For example if you wanted to run it as user www from the directory /public/www and just server html and css files, you could run it (as root) as follows:

python3 -r /public/www -u www -e html -e css

Later you could stop this process by executing
python3 -s

For more information have a look at the source code, it is pretty readable I think :-)

Availability and are both available from this GitHub repository.


  1. Good blog posts information provided
    Best Play and Pre School for kids in Hyderabad,India. To give your kid a best environment and learning it is the right way to join in play and pre school were kids can build there physically, emotionally and mentally skills developed. We provide programs to kids like Play Group, Nursery, Sanjary Junior, Sanjary Senior and Teacher training Program.
    Preschool in hyderabad

  2. Excellent explanation by the author

    Sanjary Academy is the best Piping Design institute in Hyderabad, Telangana. It is the best Piping design Course in India and we have offer professional Engineering Courses like Piping design Course, QA/QC Course, document controller course, Pressure Vessel Design Course, Welding Inspector Course, Quality Management Course and Safety Officer Course.
    Piping Design Course

  3. Thanks for providing the updated information

    Pressure Vessel Design Course is one of the courses offered by Sanjary Academy in Hyderabad. We have offer professional Engineering Course like Piping Design Course,QA / QC Course,document Controller course,pressure Vessel Design Course,Welding Inspector Course, Quality Management Course, #Safety officer course.
    Welding Inspector Course
    Safety officer course
    Quality Management Course
    Quality Management Course in India

  4. In the competitive market finding the best outsourcing services is a challenging task. This post helps me to find the best one. Thanks for sharing this post.
    Hire Magento developer India
    Hire wordpress developer India
    Hire a Programmer
    Hire PHP Developer India
    Hire Dedicated Programmers
    Mobile App Development India
    Hire Opencart Developer

  5. Thanks for sharing such a great information. Its really nice and informative chef training videos and chef devops training

  6. Wow. That is so elegant and logical and clearly explained. Brilliantly goes through what could be a complex process and makes it obvious Python Training.


  7. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care


  8. This is most informative and also this post most user friendly and super navigation to all posts. Thank you so much for giving this information to me. Python training in Chennai.

    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

  9. Really amazing experience to visit your blog, thanks a lot for sharing with us. Visit Ogen Infosystem for creative website designing and ppc services at best price in Delhi.
    PPC Company in Delhi

  10. Nice post shared! I like your content. Really it was so awesome article. Thanks for sharing nice information. Top 10 Makeup Artists in Kolkata

  11. Due to the rising demand for Data Scientists, there are many educational platforms that are coming up with programs in Data Science, you may never find the best, but you can always find the one that is suitable for you.

    Data Science Training in Gurgaon

  12. Compre documentos en línea, documentos originales y registrados.
    Acerca de Permisodeespana, algunos dicen que somos los solucionadores de problemas, mientras que otros se refieren a nosotros como vendedores de soluciones. Contamos con cientos de clientes satisfechos a nivel mundial. Hacemos documentos falsos autorizados y aprobados como Permiso de Residencia Español, DNI, Pasaporte Español y Licencia de Conducir Española. Somos los fabricantes y proveedores de primer nivel de estos documentos, reconocidos a nivel mundial.

    Comprar permiso de residencia,
    permiso de residenciareal y falso en línea,
    Compre licencia de conducir en línea,
    Compre una licencia de conducir española falsa en línea,
    Comprar tarjeta de identificación,
    Licencia de conducir real y falsa,
    Compre pasaporte real en línea,

    Visit Here fpr more information. :-
    Address: 56 Guild Street, London, EC4A 3WU (UK)
    WhatsApp: +443455280186

  13. I have been working in this industry for many years and have spoken to many students. In my opinion, iClass Gyansetu is the best institute for Selenium Training in Gurgaon

  14. You have great ideas to share something like this.Thanks for share this info.

    BCom 2nd year result

  15. i'd subsequent to to make regarded that this blog clearly satisfied me to take steps it! thanks, each and each one fine reveal... Bandicam Keygen

  16. tremendous article, thank you for putting this collectively! this is obviously one allowable publicize. thanks for the important advice and insights you have as a end result furnished right here..... Download Microsoft Office 2010 Full Crack + Keygen

  17. wordpress web design company in united states Need professional WordPress Web Design Services? We're experts in developing attractive mobile-friendly WordPress websites for businesses. Contact us today!

  18. Give your instagram a high boost and more visibility by use of relevant hashtags, posting content consistently, stick to niche etc. Learn more by watching video:

  19. Are you interested in learning Power BI and how it can help you unlock the potential of your data? Power BI Course Malaysia is the perfect way to get yourself up to speed with the latest technology. Whether you are looking for a comprehensive course to help you quickly master this powerful software or just need a refresher, we have the perfect solution for you. Explore our range of courses and take advantage of our educational resources - from tutorials and webinars to online classes and live events - so that you can gain the skills and knowledge needed to become a Power BI expert.
    power bi course malaysia


  20. Hii
    Good blog post with valuable information. If you're in Hyderabad, India, and looking for the best play and preschool for your child, consider joining a school that provides a nurturing environment for their holistic development. By enrolling your child in a play and preschool, they can enhance their physical, emotional, and mental skills in a supportive learning environment.
    Here is sharing some Kofax Training information may be its helpful to you.
    Kofax Training

  21. If you are searching for Best lasik surgeons in austin tx, your search ends here. Austin is home to a team of world-class Lasik surgeons who are widely recognized in the field for their expertise and proficiency. These surgeons are known for their unwavering commitment to patient safety and satisfaction, ensuring that every individual receives personalized care and achieves optimal vision correction results.

    Their exceptional skill set, combined with cutting-edge technology and advanced techniques, make them the preferred choice for individuals seeking superior Lasik surgery outcomes in Austin, Texas.