简体   繁体   中英

Getting the error - AttributeError: 'module' object has no attribute 'connect' - While importing from a sockets program

I'm building a GUI (using kivy) for a sockets program on python. The sockets program (client and server) are working perfectly. Now, I have to import the.connect() from the client file so that the user can connect to the server. The code for client.py is that standard telnet example that I'm using for now. When I try to import it and use it, Im getting the ATtributeError. I'm stuck here, please help, otherwise I cant move ahead of this.

'''

import socket, select, string, sys

def prompt() :
    sys.stdout.write('<You> ')
    sys.stdout.flush()

#main function
if __name__ == "__main__":

    if(len(sys.argv) < 3) :
        print('Usage : python telnet.py hostname port')
        sys.exit()

    host = sys.argv[1]
    port = int(sys.argv[2])

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2)

    # connect to remote host
    s.connect((host, port))


    print('Connected to remote host. Start sending messages')
    prompt()

    while 1:
        socket_list = [sys.stdin, s]

        # Get the list sockets which are readable
        read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])

        for sock in read_sockets:
            #incoming message from remote server
            if sock == s:
                data = sock.recv(4096)
                if not data :
                    print('\nDisconnected from chat server')
                    sys.exit()
                else :
                    #print data
                    sys.stdout.write(data)
                    prompt()

            #user entered a message
            else :
                msg = sys.stdin.readline()
                s.send(msg)
                prompt()

'''

Your code seems correct.

Check maybe you have a file called socket.py which masks the original socket module. If so, renamed it to solve the conflict.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM