Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. # TCP server example for IPv4
  2. import socket
  3. import sys
  4.  
  5.  
  6. HOST, PORT = "", 8080
  7. count = 0
  8.  
  9. def handle_client(socket, addr):
  10. # make sure that the global variable count is used
  11. # instead of creating a temporary local variable with the same name
  12. global count
  13. count = count + 1;
  14.  
  15. try:
  16. # socket.sendall( bytes( "Welcome to the server.\r\nWhat is your name please?\r\n", 'UTF-8' ) )
  17. # # We are talking to humans -- don't just read data that already arrived, wait for the reply
  18. # socket.setblocking(True)
  19. # socket.settimeout(10.0)
  20. # # string.strip removes all leading and trailing white space including \r\n
  21. # # number inside socket.recv(nnn) sets the maximum number of bytes to receive
  22. name = str( socket.recv(2048), 'UTF-8' ).strip()
  23. print("A web browser has just contacted us from " , addr , "and said: \r\n", name )
  24. # prepare the reply
  25. data = """
  26.  
  27.  
  28. HTTP/1.1 200 Ok\r\nServer: eTCP Simple Demo Server\r\n
  29. #Date: must be provided unless the server has no reliable clock - then it must not be provided
  30. Connection: close\r\n
  31. Cache-Control: no-cache\r\nExpires: 0\r\nETag: \"\"\r\n
  32. Content-Type: text/html; charset=ISO-8859-1\r\n"
  33. \r\n
  34.  
  35. #HTML Page header
  36. <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\r\n
  37. <html>\r\n<head>\r\n"
  38. <meta http-equiv=\"Content-type\" content=\"text/html; charset=iso-8859-1\">\r\n
  39. <title>A simple dynamic Web page</title></head>\r\n<body>\r\n
  40.  
  41. <p>
  42.  
  43.  
  44. </p>\r\n
  45.  
  46.  
  47. </body>\r\n</html>\r\n
  48.  
  49. """
  50.  
  51.  
  52. socket.sendall( bytes( data, 'UTF-8' ) )
  53.  
  54. data = ".\r\nYou are the " + str(count) + "th person who stopped by today. Bye now!\r\n "
  55.  
  56. socket.sendall( bytes( data, 'UTF-8' ) )
  57.  
  58. except socket.Timeouterror:
  59. socket.sendall( bytes( "You are not very talkative today. Bye now!\r\n", 'UTF-8' ) )
  60. # Note that we only handled the timeout exception. Anything else will be passed to the place this function was called
  61.  
  62. try:
  63. # Create a socket (SOCK_STREAM means a TCP socket)
  64. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  65. # Bind the socket to a particular port number (and perhaps to a network interface - HOST)
  66. server.bind( (HOST, PORT) )
  67. # Start the server
  68. server.listen(1)
  69. except OSError as msg:
  70. server.close()
  71. print("could not open the server socket")
  72. sys.exit(1)
  73.  
  74. # serve forever
  75. while 1:
  76. try:
  77. client_socket, client_address = server.accept()
  78. handle_client(client_socket, client_address)
  79. except:
  80. pass
  81. finally:
  82. client_socket.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement