Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
885 B
2
Indexable
Never
import socket

def server_program():
    #get the hostname
    host = socket.gethostname()
    port = 5000 # initiate port number above 1024

    server_socket = socket.socket() #get instance
    server_socket.bind((host, port)) #bind host addr and port together

    server_socket.listen(1)
    conn, address = server_socket.accept() #accept new connection
    print("Connection from: "+str(address))

    while True:
        #receive data stream. it won't accept data packet greater than 1024 bytes
        data = conn.recv(1024).decode()
        if not data:
            #if data is not received break
            break
        
        print('From connected user: '+ str(data))
        data = input(' -> ')
        conn.send(data.encode()) #send data to the client

    conn.close() #close the connection

if __name__ == '__main__':
    server_program()