obda.net

Simple Local Web Server with Python

Add a comment

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

Python’s standard distribution includes a simple HTTP server, which can be used to serve a local directory on the fly. Just open a terminal, change to the directory you want to share, and run:

python -m SimpleHTTPServer

Once the server is running, you will see the message:

Serving HTTP on 0.0.0.0 port 8000 ...

In your browser, you can now access the server by surfing to http://localhost:8000. Moreover, you can also access the server from other machines in your network, not only from localhost.

If you want to change the server’s port, just provide an additional argument when launching it:

python -m SimpleHTTPServer 8888

Note: The above solution works only for Python 2, as Python 3 has reorganized some modules. If you are working with the latest Python version, you need to run:

python -m http.server

or, with a different port:

python -m http.server 8888

Export Postgres Table to CSV

Add a comment

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

PostgreSQL’s psql command line client features a \copy command that allows dumping a table to a CSV file:

\copy table_name to 'filename.csv' delimiter ',' csv header

The header argument at the end will add a header line with the column names to the CSV file. Use ; as delimiter if the CSV file shall be compatible with Microsoft’s Excel.

Rotate a Video with ffmpeg

Add a comment

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

The transpose video filter of ffmpeg allows rotating a video by 90 degrees:

ffmpeg -i in.mp4 -vf 'transpose=1' out.mp4

Specify a value from 0–3 as transpose’s argument:

Value Description
0 Rotate by 90 degrees counterclockwise and flip vertically (default)
1 Rotate by 90 degrees clockwise
2 Rotate by 90 degrees counterclockwise
3 Rotate by 90 degrees clockwise and flip vertically