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.

restrictedhttpserver.py

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:


python3 restrictedhttpserver.py


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 restrictedhttpserver.py -h

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

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
                        interfaces]
  -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 restrictedhttpserver.py -r /public/www -u www -e html -e css

Later you could stop this process by executing
python3 retrictedhttpserver.py -s


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

Availability

restrictedhttpserver.py and daemon.py are both available from this GitHub repository.

41 comments:

  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

    ReplyDelete
  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.

    ReplyDelete

  7. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care
    http://chennaitraining.in/creo-training-in-chennai/
    http://chennaitraining.in/building-estimation-and-costing-training-in-chennai/
    http://chennaitraining.in/machine-learning-training-in-chennai/
    http://chennaitraining.in/data-science-training-in-chennai/
    http://chennaitraining.in/rpa-training-in-chennai/
    http://chennaitraining.in/blueprism-training-in-chennai/

    ReplyDelete

  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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. :- https://permisodeespana.com/licencia-de-conducir-espanola/
    Address: 56 Guild Street, London, EC4A 3WU (UK)
    Email: contact@permisodeespana.com
    WhatsApp: +443455280186

    ReplyDelete
  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

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

    BCom 2nd year result

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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!

    ReplyDelete
  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: https://www.youtube.com/watch?v=SE1xrciHN30

    ReplyDelete
  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

    ReplyDelete

  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

    ReplyDelete