Sat, Oct 18, 2008 – Stanford Splash
Splash: Advanced Computer Networking
In this class we learned about writing internet servers. First we looked at web servers and three different ways of writing them; second, we looked briefly at an IM server.
Below you'll find the code we discussed in class and some resources to help guide you forward.
Examples
These examples are written in Python; you can run them from a command line using:
$ python web-server.py 8080
(Replace "web-server.py" with the appropriate file name for each example, and of course, choose whatever port number you like.)
To connect to these servers, you can either use a web browser (for the web servers, anyway) and point it to http://localhost:8080/, or use telnet in a separate terminal window.
$ telnet localhost 8080
When you get to a prompt, use HTTP for the web servers, and use the IM protocol (desribed below) for the IM server.
The simplest web server ever.
No matter what request comes in, it always prints the same thing.
[web-server.py]A threaded web server.
Each new request is handled by its own thread, created byos.fork()
.
[web-server-threaded.py]An "async" web server.
Back to a single thread, but with non-blocking input and output (I/O).
[web-server-asyncore.py]An "async" IM server.
An example of writing your own protocol. Also has non-blocking I/O.
[im-server.py]
The IM Protocol
There are a number of commands you can use:
login username
— log in to the server with a specific handle. Use this command before using any others.message username message
— send message with content message to user username.broadcast message
— send a message with content message to all users.who
— get a comma-separated list of logged-in users.
Resources
There are many resources on the web to help you write a server. A good place to start is to look at the Wikipedia pages for:
If you're interested in writing servers in the Python language, be sure to check out the Python documentation. The library we used for non-blocking I/O, asyncore
, has some good references on the python.org site:
Contact Me
If you have any other questions, drop me a line at zamfire@gmail.com.