Guest User

Untitled

a guest
Jul 15th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. import select
  2. import socket
  3. import time
  4.  
  5. # Create the server.
  6. serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  7. serversocket.bind((socket.gethostname(), 7557))
  8. serversocket.listen(1)
  9.  
  10. # Wait for an incoming connection.
  11. clientsocket, address = serversocket.accept()
  12. print 'Connection from', address[0]
  13.  
  14. # Control variables.
  15. queue = []
  16. cancelled = False
  17.  
  18. while True:
  19. # If nothing queued, wait for incoming request.
  20. if not queue:
  21. queue.append(clientsocket.recv(1024))
  22.  
  23. # Receive data of length zero ==> connection closed.
  24. if len(queue[0]) == 0:
  25. break
  26.  
  27. # Get the next request and remove the trailing newline.
  28. request = queue.pop(0)[:-1]
  29. print 'Starting request', request
  30.  
  31. # Main processing loop.
  32. for i in xrange(15):
  33. # Do some of the processing.
  34. time.sleep(1.0)
  35.  
  36. # See if the socket is marked as having data ready.
  37. r, w, e = select.select((clientsocket,), (), (), 0)
  38. if r:
  39. data = clientsocket.recv(1024)
  40.  
  41. # Length of zero ==> connection closed.
  42. if len(data) == 0:
  43. cancelled = True
  44. break
  45.  
  46. # Add this request to the queue.
  47. queue.append(data)
  48. print 'Queueing request', data[:-1]
  49.  
  50. # Request was cancelled.
  51. if cancelled:
  52. print 'Request cancelled.'
  53. break
  54.  
  55. # Done with this request.
  56. print 'Request finished.'
  57.  
  58. # If we got here, the connection was closed.
  59. print 'Connection closed.'
  60. serversocket.close()
  61.  
  62. Connection from 127.0.0.1
  63. Starting request 1
  64. Queueing request 2
  65. Queueing request 3
  66. Request finished.
  67. Starting request 2
  68. Request finished.
  69. Starting request 3
  70. Request cancelled.
  71. Connection closed.
  72.  
  73. import select
  74. import socket
  75. import time
  76.  
  77. port = 7557
  78.  
  79. # Create the server.
  80. serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  81. serversocket.bind((socket.gethostname(), port))
  82. serversocket.listen(1)
  83. serverfd = serversocket.fileno()
  84. print "Listening on", socket.gethostname(), "port", port
  85.  
  86. # Make the socket non-blocking.
  87. serversocket.setblocking(0)
  88.  
  89. # Initialise the list of clients.
  90. clients = {}
  91.  
  92. # Create an epoll object and register our interest in read events on the server
  93. # socket.
  94. ep = select.epoll()
  95. ep.register(serverfd, select.EPOLLIN)
  96.  
  97. while True:
  98. # Check for events.
  99. events = ep.poll(0)
  100. for fd, event in events:
  101. # New connection to server.
  102. if fd == serverfd and event & select.EPOLLIN:
  103. # Accept the connection.
  104. connection, address = serversocket.accept()
  105. connection.setblocking(0)
  106.  
  107. # We want input notifications.
  108. ep.register(connection.fileno(), select.EPOLLIN)
  109.  
  110. # Store some information about this client.
  111. clients[connection.fileno()] = {
  112. 'delay': 0.0,
  113. 'input': "",
  114. 'response': "",
  115. 'connection': connection,
  116. 'address': address,
  117. }
  118.  
  119. # Done.
  120. print "Accepted connection from", address
  121.  
  122. # A socket was closed on our end.
  123. elif event & select.EPOLLHUP:
  124. print "Closed connection to", clients[fd]['address']
  125. ep.unregister(fd)
  126. del clients[fd]
  127.  
  128. # Error on a connection.
  129. elif event & select.EPOLLERR:
  130. print "Error on connection to", clients[fd]['address']
  131. ep.modify(fd, 0)
  132. clients[fd]['connection'].shutdown(socket.SHUT_RDWR)
  133.  
  134. # Incoming data.
  135. elif event & select.EPOLLIN:
  136. print "Incoming data from", clients[fd]['address']
  137. data = clients[fd]['connection'].recv(1024)
  138.  
  139. # Zero length = remote closure.
  140. if not data:
  141. print "Remote close on ", clients[fd]['address']
  142. ep.modify(fd, 0)
  143. clients[fd]['connection'].shutdown(socket.SHUT_RDWR)
  144.  
  145. # Store the input.
  146. else:
  147. print data
  148. clients[fd]['input'] += data
  149.  
  150. # Run when the client is ready to accept some output. The processing
  151. # loop registers for this event when the response is complete.
  152. elif event & select.EPOLLOUT:
  153. print "Sending output to", clients[fd]['address']
  154.  
  155. # Write as much as we can.
  156. written = clients[fd]['connection'].send(clients[fd]['response'])
  157.  
  158. # Delete what we have already written from the complete response.
  159. clients[fd]['response'] = clients[fd]['response'][written:]
  160.  
  161. # When all the the response is written, shut the connection.
  162. if not clients[fd]['response']:
  163. ep.modify(fd, 0)
  164. clients[fd]['connection'].shutdown(socket.SHUT_RDWR)
  165.  
  166. # Processing loop.
  167. for client in clients.keys():
  168. clients[client]['delay'] += 0.1
  169.  
  170. # When the 'processing' has finished.
  171. if clients[client]['delay'] >= 15.0:
  172. # Reverse the input to form the response.
  173. clients[client]['response'] = clients[client]['input'][::-1]
  174.  
  175. # Register for the ready-to-send event. The network loop uses this
  176. # as the signal to send the response.
  177. ep.modify(client, select.EPOLLOUT)
  178.  
  179. # Processing delay.
  180. time.sleep(0.1)
  181.  
  182. import socket
  183. ...
  184. s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
  185. s.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 1)
  186. s.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 1)
  187. s.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 5)
  188.  
  189. tcp 0 0 127.0.0.1:6666 127.0.0.1:43746 ESTABLISHED 15242/python2.6 keepalive (0.76/0/0)
  190.  
  191. 01:07:08.143052 IP localhost.6666 > localhost.43746: . ack 1 win 2048 <nop,nop,timestamp 848683438 848683188>
  192. 01:07:08.143084 IP localhost.43746 > localhost.6666: . ack 1 win 2050 <nop,nop,timestamp 848683438 848682438>
  193. 01:07:09.143050 IP localhost.6666 > localhost.43746: . ack 1 win 2048 <nop,nop,timestamp 848683688 848683438>
  194. 01:07:09.143083 IP localhost.43746 > localhost.6666: . ack 1 win 2050 <nop,nop,timestamp 848683688 848682438>
Add Comment
Please, Sign In to add comment