Question

I'm a total newbie when it comes to servers, so this question my sound silly to you, but I stucked and I need you help once more.

I have written a simple server in python, which looks like this:

#!/usr/bin/env python
from socket import *
import time
s = socket(AF_INET, SOCK_STREAM) 
s.bind(('', 8888)) 
s.listen(5)

while 1:
client,addr = s.accept() 
print 'Connected to ', addr
client.send(time.ctime(time.time())) 
client.close()

So when i write localhost:8888 in my browser, i get the message with the current server time. The next thing i want to do, is to configure my server to allow opening various files from my computer i.e. html or text ones. So when I write in my browser localhost:8888/text.html, this file opens. Where do i start with that?

I should mention I'm using linux mint and don't want to use any existing framework. I want to fully understand how the servers are working and responding.

Was it helpful?

Solution

Try this:

Create a script named webserver.py

import SimpleHTTPServer
import SocketServer

PORT = 8888

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

Create a file named text.html and place it on the same dir where your webserver.py script is.
Run python webserver.py
Navigate to http://localhost:8888/text.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top