Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import socket module
- from socket import *
- serverSocket = socket(AF_INET, SOCK_STREAM)
- serverPort = 12000
- #Prepare a sever socket
- serverSocket.bind(("", serverPort))
- serverSocket.listen(1)
- while True:
- #Establish the connection
- print 'Ready to serve...'
- connectionSocket, addr = serverSocket.accept()#Accepts a TCP client connection, waiting until connection arrives
- print 'Required connection', addr
- try:
- message = connectionSocket.recv(64)
- filename = message.split()[1]
- f = open(filename[1:])
- outputdata = f.read()
- #Send one HTTP header line into socket
- connectionSocket.send('HTTP/1.0 200 OKrnrn')
- #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
- connectionSocket.send('404 Not Found!')
- #Close client socket
- connectionSocket.close()
- serverSocket.close()
- Traceback (most recent call last):
- File "UDPServer.py", line 13, in <module>
- connectionSocket, addr = serverSocket.accept()#Accepts a TCP client connection, waiting until connection arrives
- File "C:Anacondalibsocket.py", line 202, in accept
- sock, addr = self._sock.accept()
- File "C:Anacondalibsocket.py", line 170, in _dummy
- raise error(EBADF, 'Bad file descriptor')
- socket.error: [Errno 9] Bad file descriptor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement