Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #import socket module
  2. from socket import *
  3. serverSocket = socket(AF_INET, SOCK_STREAM)
  4. serverPort = 12000
  5. #Prepare a sever socket
  6. serverSocket.bind(("", serverPort))
  7. serverSocket.listen(1)
  8.  
  9.  
  10. while True:
  11. #Establish the connection
  12. print 'Ready to serve...'
  13. connectionSocket, addr = serverSocket.accept()#Accepts a TCP client connection, waiting until connection arrives
  14. print 'Required connection', addr
  15. try:
  16.  
  17. message = connectionSocket.recv(64)
  18. filename = message.split()[1]
  19. f = open(filename[1:])
  20. outputdata = f.read()
  21.  
  22. #Send one HTTP header line into socket
  23. connectionSocket.send('HTTP/1.0 200 OKrnrn')
  24.  
  25.  
  26. #Send the content of the requested file to the client
  27.  
  28. for i in range(0, len(outputdata)):
  29. connectionSocket.send(outputdata[i])
  30. connectionSocket.close()
  31.  
  32. except IOError:
  33. #Send response message for file not found
  34. connectionSocket.send('404 Not Found!')
  35. #Close client socket
  36. connectionSocket.close()
  37. serverSocket.close()
  38.  
  39. Traceback (most recent call last):
  40. File "UDPServer.py", line 13, in <module>
  41. connectionSocket, addr = serverSocket.accept()#Accepts a TCP client connection, waiting until connection arrives
  42. File "C:Anacondalibsocket.py", line 202, in accept
  43. sock, addr = self._sock.accept()
  44. File "C:Anacondalibsocket.py", line 170, in _dummy
  45. raise error(EBADF, 'Bad file descriptor')
  46. socket.error: [Errno 9] Bad file descriptor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement