In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor gives you a detailed introduction of "how to use python socket to achieve a chat room". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use python socket to achieve a chat room" can help you solve your doubts.
Server end
Import socketimport json,structfrom concurrent.futures import ThreadPoolExecutordebug = Trues = socket.socket () s.bind (("127.0.0.1", 8848)) s.listen () clients = {} pool = ThreadPoolExecutor (100) def send_msg (soc, msg): l = len (msg.encode ("utf-8")) soc.send (struct.pack ("Q") L)) # send data soc.send (msg.encode ("utf-8")) "" to listen for messages from clients that have established a connection "" def task (ip,c): while True: l = c.recv (8) ls = struct.unpack ("Q") L) [0] data = json.loads (c.recv (ls) .decode ("utf-8")) # received a message from the client. Eg: data = {"to_addr": "msg": ""} print ("message from% s:% s"% (ip,data ["msg"])) # data sent by the client # there are two cases of data # one is sent to everyone and the other is sent separately to someone ("to_addr"): # send a dictionary in json format If this to_addr is not empty Send a private message to the target customer target_ip = data ["to_addr"] # get the target ip target_conn = clients.get (target_ip) # from the data dictionary # get the target customer's conn link send_msg (target_conn,data ["msg"]) # send message else: for ip Conn in clients.items (): # if c! = target_conn: send_msg (conn,data ["msg"]) # data ["msg"] # # find this one from all client lists and send it # to_addr = data ["to_addr"] # # print (data ["to_addr"] "_") # soc = clients.get (to_addr) # send_msg (soc,data ["msg"]) # else: # # traverse all clients and send it to everyone # for k Soc in clients.items (): # # if soc! = c: # send_msg (soc,data ["msg"]) while True: "" the following code is used to detect whether there is a client connection "" c add add = s.accept () print ("% s"% server [0], "connect to the server!") Clients [add [0]] = c # stores the ip address as key,conn in the clients dictionary as value, and clients= {"192.188.3.4": conn link} if debug: print ("clients=%s"% clients) "the following code is used to listen for messages from clients that have established a connection"pool.submit (task,add,c)"
Client end
Import socket,json,structfrom threading import Threadc = socket.socket () c.connect (("127.0.0.1", 8848)) print ("connection to server succeeded!") def recver (): while True: lens_bytes = c.recv (8) lens = struct.unpack ("Q") Lens_bytes) [0] recv_msg = c.recv (lens) .decode ("utf-8") print (recv_msg) # start threading to receive data Thread (target=recver). Start () while True: msg = input (">:"). Strip () # blocking if "@" in msg: info = {"msg": msg.split ("@") [0] "to_addr": msg.split ("@") [1]} else: info = {"msg": msg} data = json.dumps (info) .encode ("utf-8") c.send (struct.pack ("Q", len (data) c.send (data) This article "how to use python socket to implement a chat room" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.