Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import socket
  4. import os
  5. import sys
  6.  
  7.  
  8. ADDRESS = ('0.0.0.0', 12345)
  9.  
  10.  
  11. def handle(client, addr):
  12.     with client:
  13.         while True:
  14.             data = client.recv(1024)
  15.             if data:
  16.                 client.sendall(data)
  17.             else:
  18.                 print(f'Disconnected: {addr}')
  19.                 break
  20.  
  21.  
  22. if __name__ == '__main__':
  23.     if 'fork' not in dir(os):
  24.         sys.exit('Sorry :(')
  25.  
  26.     server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  27.     server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  28.  
  29.     with server:
  30.         server.bind(ADDRESS)
  31.         server.listen(socket.SOMAXCONN)
  32.  
  33.         while True:
  34.             (client, addr) = server.accept()
  35.             print(f'Got client: {addr}')
  36.  
  37.             pid = os.fork()
  38.             if pid:
  39.                 # parent
  40.                 client.close()
  41.             else:
  42.                 # child
  43.                 server.close()
  44.                 handle(client, addr)
  45.                 break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement