Python UDP套接字未接收到D

2024-05-14 19:52:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图编写一个UDP聊天系统,但由于某些原因listen()循环不起作用,我无法找出原因。在

import socket                                                                                                                                
import json
import landerdb
import threading
class PeerChat:
    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.db = landerdb.Connect("nodes")
        self.brok_ip = ""
        self.brok_port = 5000

    def listen(self):
        while True:
            msg = self.sock.recv(1024)
            print msg

    def main(self):
        while True:
            msg = raw_input("> ")
            for x in self.db.find("nodes", "all"):
                self.sock.sendto(msg, tuple(x['addr']))

    def GetNodes(self):
        self.sock.sendto("as", (self.brok_ip, self.brok_port))
        with open("nodes", 'wb') as file:
            msg, addr = self.sock.recvfrom(1024)
            print msg
            file.write(msg)

if __name__ == "__main__":
    PeerChat().GetNodes()
    threading.Thread(target=PeerChat().listen).start()
    PeerChat().main()

Tags: importselfdbmaindef原因msgsocket

热门问题