Skip to content

Instantly share code, notes, and snippets.

@JohnPhamous
Created October 21, 2018 23:33
Show Gist options
  • Select an option

  • Save JohnPhamous/b4b593d6e7ca164005ee2cca26bd99c4 to your computer and use it in GitHub Desktop.

Select an option

Save JohnPhamous/b4b593d6e7ca164005ee2cca26bd99c4 to your computer and use it in GitHub Desktop.
from socket import *
from thread import *
host = ''
port = 3983
s = socket(AF_INET, SOCK_STREAM)
s.bind((host, port))
s.listen(10)
clients = []
def client_thread(c):
try:
clients.append(c)
c.send('Wecome to the server, send me something: ')
while True:
data = c.recv(1024)
if not data:
break
if '!q' in data:
c.close()
for index, client in enumerate(clients):
if c == client:
del clients[index]
if data.split(' ')[0] == '!sendall':
for client in clients:
client.sendall('{}'.format(''.join(data.split(' ')[1:])))
else:
c.sendall('OK...{}'.format(data))
except:
print('Client disconnected')
while 1:
conn, addr = s.accept()
print('Connected with {}:{}'.format(addr[0], addr[1]))
start_new_thread(client_thread, (conn,))
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment