python server code awlak help

hecker_thama

Well-known member
  • Dec 27, 2022
    6,764
    6,977
    113
    Me mage code eka

    Python:
    import socket
    import sys
    
    #creating socket(Connect with 2 pcs)
    def create_socket():
        try:   
            global host
            global port
            global s
    
            host = "127.0.0.1"
            port = 9999
            s = socket.socket()
    
        except socket.error as msg:
            print("Socket creation error: " + str(msg))
    
    #binding the socket and listening for connections
    
    def bind_socket():
        try:
            global host
            global port
            global s
    
            print("Binding the Port: " + str(port))
    
            s.bind(host)
            s.bind(port)
            s.listen(5)
        except socket.error as msg:
            print("Socket binding error: " + str(msg) + "\n" + "Retrynig....")
    
            bind_socket()
    
    #Establish connection with the client
    
    def socket_accept():
        conn, address = s.accept()
        print("Connection has been established!" + " | IP: " + address[0] + " | Port: " + str(address))
        send_command(conn)
        conn.close()
    
    #send commands to client
    def send_command(conn):
        while True:
            terminal = input()
            if terminal == "quit":
                conn.close()
                s.close()
                sys.exit()
    
            if len(str.encode(terminal)) > 0:
                conn.send(str.encode(terminal))
                client_response = str(conn.recv(1024), "utf-8")
                print(client_response, end = "")
    
    def main():
        create_socket()
        bind_socket()
        socket_accept()
    
    main()



    error eka:-
    /usr/bin/python3.11 /homej/Documents/server programming/1/server.py
    Traceback (most recent call last):
    File "/home/Documents/server programming/1/server.py", line 63, in <module>
    main()
    File "/home/Documents/server programming/1/server.py", line 60, in main
    bind_socket()
    File "/home/Documents/server programming/1/server.py", line 28, in bind_socket
    s.bind(host)
    TypeError: bind(): AF_INET address must be tuple, not str
    Binding the Port: 9999

    Process finished with exit code 1


    Chat GPT generate karala hadawapu eka:-

    Python:
    import socket
    import sys
    
    # Creating socket (Connect with 2 pcs)
    def create_socket():
        try:
            global host
            global port
            global s
    
            host = "127.0.0.1"
            port = 9999
            s = socket.socket()
    
        except socket.error as msg:
            print("Socket creation error: " + str(msg))
    
    # Binding the socket and listening for connections
    def bind_socket():
        try:
            global host
            global port
            global s
    
            print("Binding the Port: " + str(port))
    
            # Use a tuple for the address
            s.bind((host, port))
            s.listen(5)
        except socket.error as msg:
            print("Socket binding error: " + str(msg) + "\n" + "Retrying....")
            bind_socket()
    
    # Establish connection with the client
    def socket_accept():
        conn, address = s.accept()
        print("Connection has been established! | IP: " + address[0] + " | Port: " + str(address[1]))
        send_command(conn)
        conn.close()
    
    # Send commands to client
    def send_command(conn):
        while True:
            terminal = input()
            if terminal == "quit":
                conn.close()
                s.close()
                sys.exit()
    
            if len(str.encode(terminal)) > 0:
                conn.send(str.encode(terminal))
                client_response = str(conn.recv(1024), "utf-8")
                print(client_response, end="")
    
    def main():
        create_socket()
        bind_socket()
        socket_accept()
    
    if __name__ == "__main__":
        main()

    GPT sats:-
    Here are the key changes I made:

    1. In the bind_socket() function, I updated s.bind(host) to s.bind((host, port)) to provide a tuple as the address argument to the bind() method.
    2. In the socket_accept() function, I updated the IP address and port printing to use address[1] instead of address for the port.
    3. I added a guard condition if __name__ == "__main__": around the main() function call. This ensures that the main() function is executed only if the script is run directly (not imported as a module).
    With these changes, your code should work as intended, binding the socket to the specified address and port and allowing you to send commands to the connected client.