types of a code example
Example: a
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
TCP_PORT = 8000
BUFFER_SIZE = 1024
serverSocket.bind(('', TCP_PORT))
serverSocket.listen(1)
while True:
print 'Ready to serve...'
connectionSocket, addr = serverSocket.accept()
print 'Connection address:', addr
try:
message = connectionSocket.recv(BUFFER_SIZE)
filename = message.split()[1]
f = open(filename[1:])
outputdata = f.read()
connectionSocket.send('HTTP/1.0 200 OK\r\n')
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i])
connectionSocket.close()
except IOError:
fail = '''<html> <head> <title> 404 </title> </head> <body><h1>404 Bruh</h1> <h3> hushies! </h3> </body></html>'''
connectionSocket.send('HTTP/1.0 200 OK\r\n'%len(fail))
for q in fail:
connectionSocket.send(q)
serverSocket.close()