Sign up
lakmalrupasinghe /
python-socket-code Public
Code Issues Pull requests Ac
Files master
python-socket-code / server-advance.py
lakmalrupasinghe 3 years ago
60 lines (39 loc) · 1.38 KB
Code Blame
#server code
import socket
import threading
#address information
HEADER = 64
PORT = 8098
SERVER = socket.gethostbyname(socket.gethostname(
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNET_MSG = " GOTDISCONNET"
server = socket.socket(socket.AF_INET, socket.SOC
#server.setsockopt(socket.SOL_SOCKET, socket.SO_R
server.bind(ADDR)
#purpose of the fucntion is to serve multiple cli
def incoming_clients (conn, addr):
print(" Client {addr} Connected ")
condition = True
while condition:
length_msg = conn.recv(HEADER).decode(FOR
if length_msg:
length_msg = int (length_msg)
message = conn.recv(length_msg).decod
if message == DISCONNET_MSG:
condition = False
print (f"{addr} {message}")
conn.send("Msg Received Loud and Clea
conn.close()
#the fucntion is to handle the server end
def setupServer():
server.listen() #listning funciton
print (" SLIIT Echo server is now listning :
while True:
conn, addr = server.accept()
thread = threading.Thread (target= incomi
thread.start()
print ("[Incoming Connections] {threading
print("[Evil Server] is starting .....")
setupServer()
#print (socket.gethostbyname(socket.gethostname()