Tag Archives: http

Simple Python HTTP server

Knocking up a simple and quick web server can be extremely useful without the need to install and configure a full fat web server. However, this isn't secure and will expose ALL files from the current directory executed in!

The official doc can be found here (v2) and here (v3).

NOTE: You will need to generate the certs for HTTPS version.

Python2:

HTTP

HTTPS

#!/usr/bin/env python2

import BaseHTTPServer, SimpleHTTPServer

httpd = BaseHTTPServer.HTTPServer(('<listen IP>', <port>),
        SimpleHTTPServer.SimpleHTTPRequestHandler)

httpd.serve_forever()

Or a simple one liner:

python -m SimpleHTTPServer
#!/usr/bin/env python2

import BaseHTTPServer, SimpleHTTPServer
import ssl

httpd = BaseHTTPServer.HTTPServer(('<listen IP>', <port>),
        SimpleHTTPServer.SimpleHTTPRequestHandler)

httpd.socket = ssl.wrap_socket (httpd.socket,
        keyfile="key.pem",
        certfile='cert.pem', server_side=True)

httpd.serve_forever()

Python3:

HTTP

HTTPS

#!/usr/bin/env python3

import http.server, socketserver

Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(('<listen ip>', <port>), Handler)

httpd.serve_forever()

Or a simple one liner:

python -m http.server <port> --bind '<listen ip>'
#!/usr/bin/env python3

import http.server, socketserver
import ssl

Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(('<listen ip>', <port>), Handler)

httpd.socket = ssl.wrap_socket (httpd.socket,
        keyfile="key.pem",
        certfile='cert.pem', server_side=True)

httpd.serve_forever()

HTTPS/HTTP Virtual Hosts

 

Sometimes there is a need to have vitual hosts (vhosts,Name-based virtual hosts) wikipedia

To enable https vhosts, use this example in /etc/apache2/vhosts.d/00_default_ssl_vhost.conf:

NameVirtualHost *:443

<VirtualHost *:443>
 SSLEngine on
 SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
 SSLCertificateFile /path/to/your/cert.crt
 SSLCertificateKeyFile /path/to/your/cert.key

 ServerName vhost1.cdstealer.com
 SSLOptions StrictRequire
 SSLProtocol all -SSLv2

 DocumentRoot /path/to/your/htdocs/vhost
 <Directory /path/to/your/htdocs/vhost>
 SSLRequireSSL
 Order Deny,Allow
 Allow from All
 </Directory>

 <IfModule log_config_module>
 TransferLog /var/log/apache2/ssl_access_log
 </IfModule>

</VirtualHost>

To enable http vhosts, use this example in /etc/apache2/vhosts.d/00_default_vhost.conf:

NameVirtualHost *:80

<VirtualHost *:80>
 ServerName vhost1.cdstealer.com
 DocumentRoot /path/to/your/htdocs/vhost/
 <Directory /path/to/your/htdocs/vhost/>
 Allow from All
 </Directory>
 <IfModule mpm_peruser_module>
 ServerEnvironment apache apache
 </IfModule>
</VirtualHost>

However, doing this won't be enough.  Your DNS host will also need a CNAME  to point to your server.