Minimal webserver in python

There is a minimal web server in the python standard library. To use it, change to the directory you want to serve and execute the script below:


#!/usr/bin/env python
import SimpleHTTPServer, BaseHTTPServer
except KeyboardInterrupt:
    print '^C received, bye bye!'

By default, the directory is served on port 8000, so point your browser to http://localhost:8000/ to see your freshly online website. Alternatively, you can specify the port as first argument. For example:

 > 1234

Regexp in python: extract email addresses

An example of using regular expressions in python to extract the email addresses from a text file.


#!/usr/bin/env python
Description: extract email addresses from stdin to stdout
Usage example: cat whatever.txt | | sort -fu > addr.txt
import re, sys
email_pattern = re.compile('([\w\-\.]+@(\w[\w\-]+\.)+[\w\-]+)')
for line in sys.stdin:
    # there are several matches per line
    for match in email_pattern.findall(line):
        print match[0]