#!/usr/bin/env python3
import socket
from threading import Thread, current_thread
host = "localhost"
port = 1605
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(5)
print("ready")
running=True
def clientthread(conn):
while True:
global running
buf = conn.recv(1024)
buf = buf.rstrip()
if buf.decode('utf8') == "exit":
conn.send(b"bye")
break
elif buf.decode('utf8') == "get":
conn.send(b"data")
elif buf.decode('utf8') == "adminexit":
conn.send(b"IS EXIT!")
running=False
conn.close()
break
else:
conn.send(b"listen")
def connectthread(sock):
while True:
conn, addr = sock.accept()
print("client connected with address " + addr[0])
conn.send(b"hello!")
ht = Thread(target=clientthread, args=(conn,))
ht.start()
ht2 = Thread(target=connectthread, args=(sock,))
ht2.start()
while True:
if running==False:
sock.close
print("IS EXIT!")
break
#!/usr/bin/env python3
import socket
from threading import Thread, current_thread
import sys
host = "localhost"
port = 1605
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(5)
print("ready")
running=True
def clientthread(conn):
while True:
global running
buf = conn.recv(1024)
buf = buf.rstrip()
if buf.decode('utf8') == "exit":
conn.send(b"bye")
break
elif buf.decode('utf8') == "get":
conn.send(b"data")
elif buf.decode('utf8') == "adminexit":
conn.send(b"EXIT!")
running=False
conn.close()
sys.exit()
else:
conn.send(b"listen")
def connectthread(sock):
while True:
conn, addr = sock.accept()
print("client connected with address " + addr[0])
conn.send(b"hello!")
ht = Thread(target=clientthread, args=(conn,))
ht.start()
while True:
if running==True:
ht2 = Thread(target=connectthread, args=(sock,))
ht2.start()
else:
sock.close
sys.exit()
print("IS EXIT!")
#!/usr/bin/env python3
import socket
from threading import Thread, current_thread
import sys
host = "localhost"
port = 1605
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(5)
print("ready")
def clientthread(conn):
while True:
buf = conn.recv(1024)
buf = buf.rstrip()
if buf.decode('utf8') == "exit":
conn.send(b"bye")
break
elif buf.decode('utf8') == "get":
conn.send(b"data")
elif buf.decode('utf8') == "adminexit":
conn.send(b"EXIT!")
sys.exit()
else:
conn.send(b"listen")
while True:
conn, addr = sock.accept()
print("client connected with address " + addr[0])
conn.send(b"hello!")
ht = Thread(target=clientthread, args=(conn,))
ht.start()
conn.close()
sock.close