Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import socket module
- from socket import *
- serverPort=80
- serverSocket = socket(AF_INET, SOCK_STREAM)
- #Prepare a sever socket
- #Fill in start
- serverSocket.bind(('',serverPort))
- serverSocket.listen(1)
- print('the web server is up on port:'+str(serverPort))
- #Fill in end
- while True:
- #Establish the connection
- print('Ready to serve...')
- connectionSocket, addr = serverSocket.accept()
- try:
- message = connectionSocket.recv(1024)
- print(str(message)+'::'+str(message.split()[0])+':'+str(message.split()[1]))
- filename = message.split()[1]
- print(str(filename)+'||'+str(filename[1:]))
- f = open(filename[1:])
- outputdata = f.read()
- print(outputdata)
- #Send one HTTP header line into socket
- #Fill in start
- connectionSocket.send('\nHTTP/1.1 200 OK\n\n')
- connectionSocket.send(outputdata)
- #Fill in end
- #Send the content of the requested file to the client
- for i in range(0, len(outputdata)):
- connectionSocket.send(outputdata[i])
- connectionSocket.close()
- except IOError:
- #Send response message for file not found
- #Fill in start
- connectionSocket.send('\nHTTP/1.1 404 Not Found\n\n')
- connectionSocket.send('\nHTTP/1.1 404 Not Found\n\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement