Last active
August 29, 2015 14:02
-
-
Save lidio601/0f8daf8f316f96a574a7 to your computer and use it in GitHub Desktop.
Python Web-server skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import random | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
p = random.randint(1300,1500) | |
print 'sto ascoltando sulla porta ',p | |
s.bind(('', p)) | |
s.listen(1) | |
(clis,addr) = s.accept() | |
print 'client connesso', addr | |
while 1: | |
print 'In attesa di ricevere' | |
str = clis.recv(1300) | |
if str == '' or str == None: | |
break | |
str = str.split('\r\n') | |
print str | |
print 'Mando 404' | |
sent = clis.send('''HTTP/1.1 404 Not Found\r\nDate: Wed, 02 Dec 2009 08:43:40 GMT\r\nServer: Apache/2.2.12 (Ubuntu)\r\nVary: Accept-Encoding\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\r\n<html><head>\r\n<title>404 Not Found</title>\r\n</head><body>\r\n<h1>Not Found</h1>\r\n<p>The requested URL %s was not found on this server.</p>\r\n<hr>\r\n<address>Apache/2.2.12 (Ubuntu) Server at localhost Port %s</address>\r\n</body></html>\r\n''' % (cmd,p)) | |
if sent == 0: | |
raise RuntimeError, "connessione socket interrotta" | |
break | |
clis.close() | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment