68

I seem to remember seeing a single line implementation of a webserver a couple of years ago. I'm aware of SimpleHTTPServer and it's like, and that's not it - I think this was using Socket and select().

I thought it was on the Python Tutor mailing list, but an archive search hasn't revealed anything, nor has a google search. I was wondering if anyone here might have further leads I could look up - or ideally a link to the original.

Although I guess it's entirely possible that the original author has taken it down out of shame...

1
  • I remember this one too. In the example, a single file was hosted. Upon accessing the server, the browser would download the file.
    – patzm
    Commented Nov 22, 2018 at 17:13

2 Answers 2

136

I'm pretty sure you can't have a webserver using sockets and select() on one line of code. Not even using semicolons, you'd have to have some loops and control structures.

Are you sure this isn't what you are looking for?

Python 3 version:

$ python -m http.server 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

Python 2 version: python -m SimpleHTTPServer 8000

5
  • 1
    Loops and control structures can be replaced by generators, list comprehensions, map, filter, reduce, and a load more that I won't bother listing.
    – RoadieRich
    Commented Nov 9, 2011 at 0:19
  • 2
    Wow, I guess you're right. This should be quite interesting to see.
    – Håvard
    Commented Nov 9, 2011 at 0:21
  • Use --bind with the Python3 command (only) to bind the web server to an IP address other than localhost (0.0.0.0). Be careful when doing this, though, as it exposes the structure of the current directory to anyone who can connect to your computer at port 8000 via http. If you see unknown addresses hitting the server, quit it immediately.
    – ddkilzer
    Commented May 13, 2021 at 20:13
  • @ddkilzer not sure if I'm reading your comment wrong, but running the command without --bind makes it listen on all interfaces, and --bind 0.0.0.0 isn't for localhost, instead it is the same as not using bind at all (i.e. listen on all interfaces). To only listen on localhost you should use --bind 127.0.0.1 Commented Jul 26, 2022 at 17:56
  • Where are the options documented? There is this documentation:docs.python.org/3/library/http.server.html But I am not sure how the CLI approach is supposed to work. This depends on HTTP server "main" function implementation I guess?
    – Eric Burel
    Commented Dec 13, 2022 at 8:24
2

Was it perchance perl? favourite one liners

perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'
4
  • I got this error with this: Use of ?PATTERN? without explicit operator is deprecated at -e line 1. Search pattern not terminated or ternary operator parsed as search pattern at -e line 1. Is it expected?
    – RSFalcon7
    Commented Jan 11, 2014 at 17:30
  • if you look at the link "favourite one liners", you can see that the + is just a line continuation character. The real line should be perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'
    – foozbar
    Commented Jul 11, 2014 at 7:51
  • 5
    Just remember this is vulnerable to the /../../../ hack to escape the current directory. NEVER open it off host and be wary if you have multiple users. Commented Mar 19, 2017 at 23:12
  • doesn't work. I can't see any directories being displayed. Commented Jan 14, 2019 at 0:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.