开发者

Multiple Sockets/Reuse/Close Sockets in Python? _socketobject error

so I've been trying to make this bot in python that uses sockets to get some information to and from a chat on a website. This requires a login, which gets keys, then using the keys from the first connection, you can connect to the chat (different socket/ip)

I tried doing this with socket.close() and whatnot, but alas I keep getting errors :(

This is what i'm currently using:

def send_packet(socket, packet):
    socket.send(packet+chr(0))

def socket_make(host, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    开发者_如何学编程s.connect((host, port))
    return s

socket = socket_make(get_ip('8'), get_port('8'))
send_packet(socket, '<y r="8" />')
socket.recv(1024)
send_packet(socket, '<v n="'+username+'" p="'+password+'" />')
tmp = socket.recv(1024)
socket.close()

socket = socket_make("174.36.242.40",10032)
send_packet(socket, '<y r="'+infos['c']+'" />')
token = socket.recv(1024)
token = token[token.find('i="')+3:token.find('" c="')]

It gives this Attribute error, presumably because .socket doesn't mean anything, but it normally does? I'm not quite sure

AttributeError: '_socketobject' object has no attribute 'socket'


You had imported the socket module before. Then you create a global variable with the same name ("socket"), which now takes the place of the module. The module disappeared, and is now a socket object. Then when you try to call socket_make a second time, the global socket is no longer the module, but the socket instance.

Use a different name, or better yet encapsulate your whole client into another class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜