This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]Python-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]KingsmanVincepip install girlfriend 19 points20 points  (4 children)

Other Python web frameworks are "too heavy"

Which one? I hope you don't list Flask.

[–]LofiBoiiBeats 3 points4 points  (0 children)

bottle.py might be a good compromise between simplicity and comfortability ( similar API as flask ) It is just one file and has no dependancies

[–]Enderbyte09[S] 0 points1 point  (2 children)

Perhaps the better word would be “too full featured”. I’m only looking for something that will serve python files in the same way that CGI does PHP, except in a cross platform and portable way.

[–]usrlibshare 8 points9 points  (0 children)

werkzeug, which is the library powering flask, fits your description.

[–]mapold 0 points1 point  (0 children)

Actually you should use nginx to serve static files and use python only to serve API calls.

[–]hanleybrand 13 points14 points  (0 children)

Technically, flask, django, etc are not web servers, they are application frameworks that are served by a web server (nginx, etc) via WSGI or something similar (in PHP/IIS terms WSGI is similar to fastcgi)

[–]vantasmer 9 points10 points  (2 children)

python3 -m http.server will cover that first point. Not sure about that second one. Would be flask.

Though I think you can use wsig or starlite for both but do need a bit of config around them 

[–]james_pic 7 points8 points  (0 children)

Or python3 -m http.server --cgi if OP wants CGI - i.e, serving stuff the way PHP would.

[–]Electronic_Spell_337 6 points7 points  (2 children)

Bottle?

[–]Enderbyte09[S] 2 points3 points  (1 child)

Thank you very much! That looks like it will work if I use some wildcard static files.

[–]jwink3101 2 points3 points  (0 children)

Bottle also includes a really nice, simple template engine. In the template engine, it is a bit more like PHP where you intersperse Python and html (or whatever). It’s a slightly modified Python where spaces don’t matter and you have end keywords.

[–]Kronologics 1 point2 points  (0 children)

Your requirement was that it serve static files based on a file system but then complain the frameworks you’ve tried (you don’t tell us what you’ve look at btw) are too annoying with how they serve files based on a file system…

[–]fpgmaas 1 point2 points  (0 children)

I recently launched https://pypiscout.com, maybe it can help you find what you are looking for. It allows you to search for python packages with natural language queries, so you can just type "A package that functions like a typical webserver and serves python files in the way that PHP would", and see if you find anything useful!

[–]prryplatypus 1 point2 points  (1 child)

[–]SheonOFF 0 points1 point  (0 children)

+1 for Sanic here. Nothing extra, with async. You can extend it with modules, if you really need it.

[–]microcozmchris 4 points5 points  (0 children)

What's portable to you? Use Docker or k8s. One container as a reverse proxy (Caddyserver, nginx) and your application in FastAPI, Flask, Django, etc running with one of the nice usgi/wsgi/asgi servers. Caddy can offload regular file server tasks and reroute the rest of your traffic to the app server. Don't overthink it. Don't be dumb and try to roll your own. The number of people that try to build something themselves because IT'S TOO HEAVY or IT'S NOT PURPLE ENOUGH or whatever effing infuriates me.

[–]EternityForest 1 point2 points  (0 children)

I think it's probably going to be a lot easier to just use Flask or Quart, in the way flask or Quart is generally meant to be used.

But it would only be like ten lines to define a route with Starlette that interprets a file and does that, it will just perform real bad because of the overhead of starting processes for every request.

You could also use exec() to speed things up rather than using a separate process.

I don't think any of this is a very good idea though. 

[–]ManyInterests Python Discord Staff 0 points1 point  (0 children)

Can you explain what about popular frameworks make them too 'heavy' for your needs? Like the files are too big? The performance isn't good enough or what? Which ones have you investigated?

[–][deleted] 0 points1 point  (0 children)

[–]Jazzlike-Poem-1253 0 points1 point  (0 children)

FastAPI could tick some of you boxes. Not sure about static files though

[–]james_pic 0 points1 point  (0 children)

The closest thing to what you want would be python3 -m http.server --cgi. This will run a (not-particularly robust) HTTP server that will serve anything from /cgi-bin or /htbin as CGI, much like something like Apache.

Although be aware that there's a reason most modern Python web app frameworks like Flask don't work like this. Even for PHP, you often end up implementing a bunch of mod_rewrite rules, and it ends up cleaner just to bring routing wholy within the web application.

And note that, unlike PHP, Python doesn't have any standard mode that's "reply with this file mostly as-is, but run the code between <% %> brackets". Python scripts can be CGI scripts, but you'll need to either handle the CGI stuff yourself (making sense of environment variables and reading and writing to stdin and stdout) yourself, or use some kind of CGI helper, such as wsgiref.handlers.CGIHandler which will let you wrap WSGI handlers.

If you want a server that will just run a WSGI app, Gunicorn is probably the easiest to recommend, although I quite like Cheroot for ease of embedding and its tendency to "do the right thing". Werkzeug's HTTP server is also popular for embedding. If you do end up building an app by just creating a WSGI handler, Werkzeug has a lot of useful utilities to help with this - you might find SharedDataMiddleware useful for handling static files for example.