basic_protocols.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. #!/usr/bin/python3
  2. #
  3. import socket
  4. import struct
  5. import logging
  6. import time
  7. import enum
  8. import select
  9. import os
  10. #
  11. import accelerated_functions
  12. #
  13. class ProtocolException(Exception):
  14. pass
  15. #
  16. class ProtocolHelper():
  17. def __init__(self):
  18. self._buffer = b''
  19. #
  20. def set_buffer(self, data):
  21. """
  22. Set the buffer contents to the data that you wish to send.
  23. """
  24. #
  25. self._buffer = data
  26. #
  27. def get_buffer(self):
  28. return self._buffer
  29. #
  30. def recv(self, socket, num_bytes):
  31. """
  32. Try to fill up the buffer to a max of 'num_bytes'. If the buffer is filled,
  33. return True, otherwise return False.
  34. """
  35. #
  36. data = socket.recv(num_bytes-len(self._buffer))
  37. #
  38. if len(data) == 0:
  39. raise ProtocolException('The socket was closed.')
  40. #
  41. self._buffer += data
  42. if len(self._buffer) == num_bytes:
  43. return True
  44. #
  45. return False
  46. #
  47. def send(self, socket):
  48. """
  49. Try to send the remainder of the buffer. If the entire buffer has been sent,
  50. return True, otherwise return False.
  51. """
  52. #
  53. n = socket.send(self._buffer)
  54. self._buffer = self._buffer[n:]
  55. if len(self._buffer) == 0:
  56. return True
  57. #
  58. return False
  59. #
  60. #
  61. class Protocol():
  62. def _run_iteration(self):
  63. """
  64. This function should be overridden. It runs a single iteration of the protocol.
  65. """
  66. #
  67. pass
  68. #
  69. def run(self):
  70. while True:
  71. finished = self._run_iteration()
  72. #
  73. if finished:
  74. # protocol is done
  75. return True
  76. #
  77. #
  78. #
  79. #
  80. class FakeProxyProtocol(Protocol):
  81. def __init__(self, socket, addr_port):
  82. self.socket = socket
  83. self.addr_port = addr_port
  84. #
  85. self.states = enum.Enum('PROXY_STATES', 'READY_TO_BEGIN CONNECTING_TO_PROXY DONE')
  86. self.state = self.states.READY_TO_BEGIN
  87. #
  88. self.protocol_helper = None
  89. #
  90. def _run_iteration(self):
  91. if self.state is self.states.READY_TO_BEGIN:
  92. self.protocol_helper = ProtocolHelper()
  93. host, port = self.addr_port
  94. addr = socket.inet_aton(host)[::-1]
  95. self.protocol_helper.set_buffer(addr+struct.pack('!H', port))
  96. self.state = self.states.CONNECTING_TO_PROXY
  97. #
  98. if self.state is self.states.CONNECTING_TO_PROXY:
  99. if self.protocol_helper.send(self.socket):
  100. self.protocol_helper = ProtocolHelper()
  101. self.state = self.states.DONE
  102. #
  103. #
  104. if self.state is self.states.DONE:
  105. return True
  106. #
  107. return False
  108. #
  109. #
  110. class ChainedProtocol(Protocol):
  111. def __init__(self, protocols):
  112. self.protocols = protocols
  113. self.current_protocol = 0
  114. #
  115. self.states = enum.Enum('CHAIN_STATES', 'READY_TO_BEGIN RUNNING DONE')
  116. self.state = self.states.READY_TO_BEGIN
  117. #
  118. def _run_iteration(self):
  119. if self.state is self.states.READY_TO_BEGIN:
  120. self.state = self.states.RUNNING
  121. #
  122. if self.state is self.states.RUNNING:
  123. if self.protocols[self.current_protocol] is None or self.protocols[self.current_protocol].run():
  124. self.current_protocol += 1
  125. #
  126. if self.current_protocol >= len(self.protocols):
  127. self.state = self.states.DONE
  128. #
  129. #
  130. if self.state is self.states.DONE:
  131. return True
  132. #
  133. return False
  134. #
  135. #
  136. class Socks4Protocol(Protocol):
  137. def __init__(self, socket, addr_port, username=None):
  138. self.socket = socket
  139. self.addr_port = addr_port
  140. self.username = username
  141. #
  142. self.states = enum.Enum('SOCKS_4_STATES', 'READY_TO_BEGIN CONNECTING_TO_PROXY WAITING_FOR_PROXY DONE')
  143. self.state = self.states.READY_TO_BEGIN
  144. #
  145. self.protocol_helper = None
  146. #
  147. def _run_iteration(self):
  148. if self.state is self.states.READY_TO_BEGIN:
  149. self.protocol_helper = ProtocolHelper()
  150. self.protocol_helper.set_buffer(self.socks_cmd(self.addr_port, self.username))
  151. self.state = self.states.CONNECTING_TO_PROXY
  152. #
  153. if self.state is self.states.CONNECTING_TO_PROXY:
  154. if self.protocol_helper.send(self.socket):
  155. self.protocol_helper = ProtocolHelper()
  156. self.state = self.states.WAITING_FOR_PROXY
  157. #logging.debug('Waiting for reply from proxy')
  158. #
  159. #
  160. if self.state is self.states.WAITING_FOR_PROXY:
  161. response_size = 8
  162. if self.protocol_helper.recv(self.socket, response_size):
  163. response = self.protocol_helper.get_buffer()
  164. if response[1] != 0x5a:
  165. raise ProtocolException('Could not connect to SOCKS proxy, msg: %x'%(response[1],))
  166. #
  167. self.state = self.states.DONE
  168. #
  169. #
  170. if self.state is self.states.DONE:
  171. return True
  172. #
  173. return False
  174. #
  175. def socks_cmd(self, addr_port, username=None):
  176. socks_version = 4
  177. command = 1
  178. dnsname = b''
  179. host, port = addr_port
  180. #
  181. try:
  182. username = bytes(username, 'utf8')
  183. except TypeError:
  184. pass
  185. #
  186. if username is None:
  187. username = b''
  188. elif b'\x00' in username:
  189. raise ProtocolException('Username cannot contain a NUL character.')
  190. #
  191. username = username+b'\x00'
  192. #
  193. try:
  194. addr = socket.inet_aton(host)
  195. except socket.error:
  196. addr = b'\x00\x00\x00\x01'
  197. dnsname = bytes(host, 'utf8')+b'\x00'
  198. #
  199. return struct.pack('!BBH', socks_version, command, port) + addr + username + dnsname
  200. #
  201. #
  202. class PushDataProtocol(Protocol):
  203. def __init__(self, socket, total_bytes, send_buffer_len=None, use_acceleration=None, push_start_cb=None, push_done_cb=None):
  204. if send_buffer_len is None:
  205. send_buffer_len = 1024*512
  206. #
  207. if use_acceleration is None:
  208. use_acceleration = True
  209. #
  210. self.socket = socket
  211. self.total_bytes = total_bytes
  212. self.use_acceleration = use_acceleration
  213. self.push_start_cb = push_start_cb
  214. self.push_done_cb = push_done_cb
  215. #
  216. self.states = enum.Enum('PUSH_DATA_STATES', 'READY_TO_BEGIN SEND_INFO START_CALLBACK PUSH_DATA RECV_CONFIRMATION DONE_CALLBACK DONE')
  217. self.state = self.states.READY_TO_BEGIN
  218. #
  219. self.byte_buffer = os.urandom(send_buffer_len)
  220. self.bytes_written = 0
  221. self.protocol_helper = None
  222. #
  223. def _run_iteration(self):
  224. if self.state is self.states.READY_TO_BEGIN:
  225. info = self.total_bytes.to_bytes(8, byteorder='big', signed=False)
  226. info += len(self.byte_buffer).to_bytes(8, byteorder='big', signed=False)
  227. self.protocol_helper = ProtocolHelper()
  228. self.protocol_helper.set_buffer(info)
  229. self.state = self.states.SEND_INFO
  230. #
  231. if self.state is self.states.SEND_INFO:
  232. if self.protocol_helper.send(self.socket):
  233. self.state = self.states.START_CALLBACK
  234. #
  235. #
  236. if self.state is self.states.START_CALLBACK:
  237. if self.push_start_cb is not None:
  238. self.push_start_cb()
  239. #
  240. self.state = self.states.PUSH_DATA
  241. #
  242. if self.state is self.states.PUSH_DATA:
  243. if self.use_acceleration:
  244. ret_val = accelerated_functions.push_data(self.socket.fileno(), self.total_bytes, self.byte_buffer)
  245. if ret_val < 0:
  246. raise ProtocolException('Error while pushing data.')
  247. #
  248. self.bytes_written = self.total_bytes
  249. else:
  250. bytes_remaining = self.total_bytes-self.bytes_written
  251. data_size = min(len(self.byte_buffer), bytes_remaining)
  252. if data_size != len(self.byte_buffer):
  253. data = self.byte_buffer[:data_size]
  254. else:
  255. data = self.byte_buffer
  256. # don't make a copy of the byte string each time if we don't need to
  257. #
  258. n = self.socket.send(data)
  259. self.bytes_written += n
  260. #
  261. if self.bytes_written == self.total_bytes:
  262. # finished sending the data
  263. logging.debug('Finished sending the data (%d bytes).', self.bytes_written)
  264. self.protocol_helper = ProtocolHelper()
  265. self.state = self.states.RECV_CONFIRMATION
  266. #
  267. #
  268. if self.state is self.states.RECV_CONFIRMATION:
  269. response_size = 8
  270. if self.protocol_helper.recv(self.socket, response_size):
  271. response = self.protocol_helper.get_buffer()
  272. if response != b'RECEIVED':
  273. raise ProtocolException('Did not receive the expected message: {}'.format(response))
  274. #
  275. self.state = self.states.DONE_CALLBACK
  276. #
  277. #
  278. if self.state is self.states.DONE_CALLBACK:
  279. if self.push_done_cb is not None:
  280. self.push_done_cb()
  281. #
  282. self.state = self.states.DONE
  283. #
  284. if self.state is self.states.DONE:
  285. return True
  286. #
  287. return False
  288. #
  289. #
  290. class PullDataProtocol(Protocol):
  291. def __init__(self, socket, use_acceleration=None):
  292. if use_acceleration is None:
  293. use_acceleration = True
  294. #
  295. self.socket = socket
  296. self.use_acceleration = use_acceleration
  297. #
  298. self.states = enum.Enum('PULL_DATA_STATES', 'READY_TO_BEGIN RECV_INFO PULL_DATA SEND_CONFIRMATION DONE')
  299. self.state = self.states.READY_TO_BEGIN
  300. #
  301. self.data_size = None
  302. self.recv_buffer_len = None
  303. self.bytes_read = 0
  304. self.protocol_helper = None
  305. self.time_of_first_byte = None
  306. self.time_of_last_byte = None
  307. #self.byte_counter = None
  308. #self.byte_counter_start_time = None
  309. self.deltas = None
  310. #
  311. def _run_iteration(self):
  312. if self.state is self.states.READY_TO_BEGIN:
  313. self.protocol_helper = ProtocolHelper()
  314. self.state = self.states.RECV_INFO
  315. #
  316. if self.state is self.states.RECV_INFO:
  317. info_size = 16
  318. if self.protocol_helper.recv(self.socket, info_size):
  319. response = self.protocol_helper.get_buffer()
  320. self.data_size = int.from_bytes(response[0:8], byteorder='big', signed=False)
  321. self.recv_buffer_len = int.from_bytes(response[8:16], byteorder='big', signed=False)
  322. assert(self.recv_buffer_len <= 10*1024*1024)
  323. # don't use a buffer size larget than 10 MiB to avoid using up all memory
  324. self.state = self.states.PULL_DATA
  325. #
  326. #
  327. if self.state is self.states.PULL_DATA:
  328. if self.use_acceleration:
  329. #(ret_val, time_of_first_byte, time_of_last_byte, byte_counter, byte_counter_start_time) = accelerated_functions.pull_data(self.socket.fileno(), self.data_size, self.recv_buffer_len)
  330. (ret_val, time_of_first_byte, time_of_last_byte, deltas) = accelerated_functions.pull_data(self.socket.fileno(), self.data_size, self.recv_buffer_len)
  331. if ret_val < 0:
  332. raise ProtocolException('Error while pulling data.')
  333. #
  334. #if sum(byte_counter) != self.data_size:
  335. if sum(deltas['bytes']) != self.data_size:
  336. logging.warning('Lost some history data ({} != {}).'.format(sum(deltas['bytes']), self.data_size))
  337. #
  338. self.bytes_read = self.data_size
  339. self.time_of_first_byte = time_of_first_byte
  340. self.time_of_last_byte = time_of_last_byte
  341. #self.byte_counter = byte_counter
  342. #self.byte_counter_start_time = byte_counter_start_time
  343. self.deltas = deltas
  344. else:
  345. bytes_remaining = self.data_size-self.bytes_read
  346. block_size = min(self.recv_buffer_len, bytes_remaining)
  347. #
  348. data = self.socket.recv(block_size)
  349. #
  350. if len(data) == 0:
  351. raise ProtocolException('The socket was closed.')
  352. #
  353. self.bytes_read += len(data)
  354. #
  355. if self.bytes_read != 0 and self.time_of_first_byte is None:
  356. self.time_of_first_byte = time.time()
  357. #
  358. if self.bytes_read == self.data_size and self.time_of_last_byte is None:
  359. self.time_of_last_byte = time.time()
  360. #
  361. #
  362. if self.bytes_read == self.data_size:
  363. # finished receiving the data
  364. logging.debug('Finished receiving the data.')
  365. self.protocol_helper = ProtocolHelper()
  366. self.protocol_helper.set_buffer(b'RECEIVED')
  367. self.state = self.states.SEND_CONFIRMATION
  368. #
  369. #
  370. if self.state is self.states.SEND_CONFIRMATION:
  371. if self.protocol_helper.send(self.socket):
  372. self.state = self.states.DONE
  373. #
  374. #
  375. if self.state is self.states.DONE:
  376. return True
  377. #
  378. return False
  379. #
  380. def calc_transfer_rate(self):
  381. """ Returns bytes/s. """
  382. assert self.data_size is not None and self.time_of_first_byte is not None and self.time_of_last_byte is not None
  383. try:
  384. return self.data_size/(self.time_of_last_byte-self.time_of_first_byte)
  385. except ZeroDivisionError:
  386. return float('nan')
  387. #
  388. #
  389. #
  390. class SendDataProtocol(Protocol):
  391. def __init__(self, socket, data):
  392. self.socket = socket
  393. self.send_data = data
  394. #
  395. self.states = enum.Enum('SEND_DATA_STATES', 'READY_TO_BEGIN SEND_INFO SEND_DATA RECV_CONFIRMATION DONE')
  396. self.state = self.states.READY_TO_BEGIN
  397. #
  398. self.protocol_helper = None
  399. #
  400. def _run_iteration(self):
  401. if self.state is self.states.READY_TO_BEGIN:
  402. info_size = 20
  403. info = len(self.send_data).to_bytes(info_size, byteorder='big', signed=False)
  404. self.protocol_helper = ProtocolHelper()
  405. self.protocol_helper.set_buffer(info)
  406. self.state = self.states.SEND_INFO
  407. #
  408. if self.state is self.states.SEND_INFO:
  409. if self.protocol_helper.send(self.socket):
  410. self.protocol_helper = ProtocolHelper()
  411. if len(self.send_data) > 0:
  412. self.protocol_helper.set_buffer(self.send_data)
  413. self.state = self.states.SEND_DATA
  414. else:
  415. self.state = self.states.RECV_CONFIRMATION
  416. #
  417. #
  418. #
  419. if self.state is self.states.SEND_DATA:
  420. if self.protocol_helper.send(self.socket):
  421. self.protocol_helper = ProtocolHelper()
  422. self.state = self.states.RECV_CONFIRMATION
  423. #
  424. #
  425. if self.state is self.states.RECV_CONFIRMATION:
  426. response_size = 8
  427. if self.protocol_helper.recv(self.socket, response_size):
  428. response = self.protocol_helper.get_buffer()
  429. if response != b'RECEIVED':
  430. raise ProtocolException('Did not receive the expected message: {}'.format(response))
  431. #
  432. self.state = self.states.DONE
  433. #
  434. #
  435. if self.state is self.states.DONE:
  436. return True
  437. #
  438. return False
  439. #
  440. #
  441. class ReceiveDataProtocol(Protocol):
  442. def __init__(self, socket):
  443. self.socket = socket
  444. #
  445. self.states = enum.Enum('RECV_DATA_STATES', 'READY_TO_BEGIN RECV_INFO RECV_DATA SEND_CONFIRMATION DONE')
  446. self.state = self.states.READY_TO_BEGIN
  447. #
  448. self.protocol_helper = None
  449. self.data_size = None
  450. self.received_data = None
  451. #
  452. def _run_iteration(self):
  453. if self.state is self.states.READY_TO_BEGIN:
  454. self.protocol_helper = ProtocolHelper()
  455. self.state = self.states.RECV_INFO
  456. #
  457. if self.state is self.states.RECV_INFO:
  458. info_size = 20
  459. if self.protocol_helper.recv(self.socket, info_size):
  460. response = self.protocol_helper.get_buffer()
  461. self.data_size = int.from_bytes(response, byteorder='big', signed=False)
  462. self.protocol_helper = ProtocolHelper()
  463. if self.data_size > 0:
  464. self.state = self.states.RECV_DATA
  465. else:
  466. self.received_data = b''
  467. self.protocol_helper.set_buffer(b'RECEIVED')
  468. self.state = self.states.SEND_CONFIRMATION
  469. #
  470. #
  471. #
  472. if self.state is self.states.RECV_DATA:
  473. if self.protocol_helper.recv(self.socket, self.data_size):
  474. response = self.protocol_helper.get_buffer()
  475. self.received_data = response
  476. self.protocol_helper = ProtocolHelper()
  477. self.protocol_helper.set_buffer(b'RECEIVED')
  478. self.state = self.states.SEND_CONFIRMATION
  479. #
  480. #
  481. if self.state is self.states.SEND_CONFIRMATION:
  482. if self.protocol_helper.send(self.socket):
  483. self.state = self.states.DONE
  484. #
  485. #
  486. if self.state is self.states.DONE:
  487. return True
  488. #
  489. return False
  490. #
  491. #
  492. class ServerListener():
  493. def __init__(self, endpoint, accept_callback):
  494. self.callback = accept_callback
  495. #
  496. self.s = socket.socket()
  497. self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  498. self.s.bind(endpoint)
  499. self.s.listen(0)
  500. #
  501. def accept(self):
  502. newsock, endpoint = self.s.accept()
  503. logging.debug("New client from %s:%d (fd=%d)",
  504. endpoint[0], endpoint[1], newsock.fileno())
  505. self.callback(newsock)
  506. #
  507. #
  508. class SimpleClientConnectionProtocol(Protocol):
  509. def __init__(self, endpoint, total_bytes, data_generator=None, proxy=None, username=None):
  510. self.endpoint = endpoint
  511. self.data_generator = data_generator
  512. self.total_bytes = total_bytes
  513. self.proxy = proxy
  514. self.username = username
  515. #
  516. self.states = enum.Enum('CLIENT_CONN_STATES', 'READY_TO_BEGIN CONNECT_TO_PROXY PUSH_DATA DONE')
  517. self.state = self.states.READY_TO_BEGIN
  518. #
  519. self.socket = socket.socket()
  520. self.sub_protocol = None
  521. #
  522. if self.proxy is None:
  523. logging.debug('Socket %d connecting to endpoint %r...', self.socket.fileno(), self.endpoint)
  524. self.socket.connect(self.endpoint)
  525. else:
  526. logging.debug('Socket %d connecting to proxy %r...', self.socket.fileno(), self.proxy)
  527. self.socket.connect(self.proxy)
  528. #
  529. #
  530. def _run_iteration(self):
  531. if self.state is self.states.READY_TO_BEGIN:
  532. if self.proxy is None:
  533. self.sub_protocol = PushDataProtocol(self.socket, self.total_bytes, self.data_generator)
  534. self.state = self.states.PUSH_DATA
  535. else:
  536. self.sub_protocol = Socks4Protocol(self.socket, self.endpoint, username=self.username)
  537. self.state = self.states.CONNECT_TO_PROXY
  538. #
  539. #
  540. if self.state is self.states.CONNECT_TO_PROXY:
  541. if self.sub_protocol.run():
  542. self.sub_protocol = PushDataProtocol(self.socket, self.total_bytes, self.data_generator)
  543. self.state = self.states.PUSH_DATA
  544. #
  545. #
  546. if self.state is self.states.PUSH_DATA:
  547. if self.sub_protocol.run():
  548. self.state = self.states.DONE
  549. #
  550. #
  551. if self.state is self.states.DONE:
  552. return True
  553. #
  554. return False
  555. #
  556. #
  557. class SimpleServerConnectionProtocol(Protocol):
  558. def __init__(self, socket, conn_id, bandwidth_callback=None):
  559. self.socket = socket
  560. self.conn_id = conn_id
  561. self.bandwidth_callback = bandwidth_callback
  562. #
  563. self.states = enum.Enum('SERVER_CONN_STATES', 'READY_TO_BEGIN PULL_DATA DONE')
  564. self.state = self.states.READY_TO_BEGIN
  565. #
  566. self.sub_protocol = None
  567. #
  568. def _run_iteration(self):
  569. if self.state is self.states.READY_TO_BEGIN:
  570. self.sub_protocol = PullDataProtocol(self.socket)
  571. self.state = self.states.PULL_DATA
  572. #
  573. if self.state is self.states.PULL_DATA:
  574. if self.sub_protocol.run():
  575. if self.bandwidth_callback:
  576. self.bandwidth_callback(self.conn_id, self.sub_protocol.data_size, self.sub_protocol.calc_transfer_rate())
  577. #
  578. self.state = self.states.DONE
  579. #
  580. #
  581. if self.state is self.states.DONE:
  582. return True
  583. #
  584. return False
  585. #
  586. #
  587. if __name__ == '__main__':
  588. import sys
  589. logging.basicConfig(level=logging.DEBUG)
  590. #
  591. if sys.argv[1] == 'client':
  592. endpoint = ('127.0.0.1', 4747)
  593. #proxy = ('127.0.0.1', 9003)
  594. proxy = None
  595. username = bytes([x for x in os.urandom(12) if x != 0])
  596. #username = None
  597. data_MB = 4000
  598. #
  599. client = SimpleClientConnectionProtocol(endpoint, data_MB*2**20, proxy=proxy, username=username)
  600. client.run()
  601. elif sys.argv[1] == 'server':
  602. import multiprocessing
  603. import queue
  604. #
  605. endpoint = ('127.0.0.1', 4747)
  606. processes = []
  607. conn_counter = [0]
  608. #
  609. def bw_callback(conn_id, data_size, transfer_rate):
  610. logging.info('Avg Transferred (MB): %.4f', data_size/(1024**2))
  611. logging.info('Avg Transfer rate (MB/s): %.4f', transfer_rate/(1024**2))
  612. #
  613. def start_server_conn(socket, conn_id):
  614. server = SimpleServerConnectionProtocol(socket, conn_id, bandwidth_callback=bw_callback)
  615. try:
  616. server.run()
  617. except KeyboardInterrupt:
  618. socket.close()
  619. #
  620. #
  621. def accept_callback(socket):
  622. conn_id = conn_counter[0]
  623. conn_counter[0] += 1
  624. #
  625. p = multiprocessing.Process(target=start_server_conn, args=(socket, conn_id))
  626. processes.append(p)
  627. p.start()
  628. #
  629. l = ServerListener(endpoint, accept_callback)
  630. #
  631. try:
  632. while True:
  633. l.accept()
  634. #
  635. except KeyboardInterrupt:
  636. print()
  637. #
  638. for p in processes:
  639. p.join()
  640. #
  641. #
  642. #