63

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

RoadieRich
  • 6,330
  • 3
  • 35
  • 52
  • I remember this one too. In the example, a single file was hosted. Upon accessing the server, the browser would download the file. – patzm Nov 22 '18 at 17:13

2 Answers2

126

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

Zitrax
  • 19,036
  • 20
  • 88
  • 110
Håvard
  • 9,900
  • 1
  • 41
  • 46
  • 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 Nov 09 '11 at 00:19
  • 2
    Wow, I guess you're right. This should be quite interesting to see. – Håvard Nov 09 '11 at 00: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 May 13 '21 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` – Kristopher Noronha Jul 26 '22 at 17:56
  • Where are the options documented? There is this documentation:https://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 Dec 13 '22 at 08: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 \/(.*) / })'
Kinjal Dixit
  • 7,777
  • 2
  • 59
  • 68
  • 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 Jan 11 '14 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 Jul 11 '14 at 07: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. – user3710044 Mar 19 '17 at 23:12
  • doesn't work. I can't see any directories being displayed. – User9102d82 Jan 14 '19 at 00:30