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--
This article mainly introduces how to use python code to achieve simple chat room related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this article on how to use python code to achieve a simple chat room will have a harvest, let's take a look.
1. Function: similar to qq group chat function
1. Someone who enters a chat room needs to enter a name, and the name can not be repeated.
two。 If someone enters the chat room, others will be notified.
Xxx enters the chat room
3. If one person sends a message, others will receive a message.
Xxx:xxxxxxxx
4. If someone quits the chat room, others will be notified.
Xxx quits the chat room
5. Extended function: server-side message announcement, server-side message sent by all can be received
Administrator message: xxxxxxxx
two。 Determine the technical model
1. Server and client
The server processes the request and sends the administrator message
The client performs various functions
two。 Socket selection: udp socket
3. Message delivery model: forwarding
Client ~ > server ~ > other client
4. Store user information: {name:addr}
5. Deal with the relationship between sending and receiving: multiple processes deal with sending and receiving separately
3. Matters needing attention
1. Design package scheme
two。 Write a functional module to test a module
3. Pay attention to the addition of comments
# coding = utf-8 "" chat roomenv:python3.5exc:socket and forkname:mianmabbemail:mianmabb@163.com server function: 1. Set up network communication II. The process enters the chat room * receives the name * determines whether it is allowed to enter * feedback the result to the client * if it ends if it is not allowed, the user is allowed to insert the data structure * send notification 3 to others. Deal with chat * receive messages, determine the type of messages, divided into L (input name), C (send messages), Q (exit chat room) * forward messages 4. Deal with quitting chat room 5. Send the administrator message "" from socket import * from os import * from sys import * user = {} # create an empty dictionary to store the user's nickname and address # process login def do_login (sMagna name addr): if name in user: # determine whether the nickname already exists s.sendto ("the nickname is occupied" .encode (), addr) return else: # nickname does not exist Then send the agreed "OK" s.sendto (b "OK", addr) # function: if someone enters the chat room, others will receive the message msg = "Welcome% s to the chat room"% name for i in user: # send the message to other users s.sendto (msg.encode ()) User [I]) user [name] = addr # insert the user into the data structure (dictionary) # handle chat def do_chat (s:% s "% s"% (name,text) # set message display format for i in user: s.sendto (msg.encode (), user [I]) # process exit def do_quit (s Name): msg = "% s exited the chat room"% name for i in user: if I! = name: # send the user exit message s.sendto (msg.encode (), user [I]) else: # send the agreed EXIT to the user client for the parent process to exit s.sendto (b "EXIT") User [I]) del user [name] # Delete the user in the dictionary # process request def do_request (s): # accept all customer requests in a loop while True: try: data Addr = s.recvfrom (1024) except KeyboardInterrupt: # capture parent process exit error exit ("server exit!") # print (data.decode ()) msgList = data.decode (). Split () # split the list by space, making it easy to index if msgList [0] = = "L": # to determine the message type do_login (msgList [1]) Addr) elif msgList [0] = = "C": text = ".join (msgList [2:]) # add the spaces that may be in the message back to do_chat (msgList [1], text) elif msgList [0] =" Q ": do_quit (msgList [1]) def main (): s = socket (AF_INET) SOCK_DGRAM) ADDR = ("0.0.0.0") 8888) s.bind (ADDR) # creation process pid = fork () if pid < 0: print ("Error") elif pid = 0: # the child process is used to send administrator messages while True: try: text = input ("administrator:") except KeyboardInterrupt: # capture child process exits directly Error exit () msg = "C administrator% s"% text s.sendto (msg.encode () ADDR) else: # parent process is used to process the request do_request (s) main ()
Client features:
1. Set up communication 2. Enter the chat room * enter the name * send it to the server * receive feedback from the server * re-enter if you are not allowed to enter the chat room * create a new process for sending and receiving messages 3. Chat * cycle send message types are divided into L (input name), C (send message), Q (exit chat room) * cycle to receive message 4. Quit the chat room 5. Accept administrator message "" from socket import * from os import * from sys import * ADDR = ("127.0.0.1", 8888) # fill in the server address # send the message def send_msg (s) in a loop Name): while True: try: text = input () # customer enters the message to be sent except KeyboardInterrupt: # subprocess prevents user Ctrl+C from exiting directly text = "quit" if text.strip () = "quit": # enter quit to exit msg = "Q" + name # message type Name s.sendto (msg.encode (), ADDR) exit ("you have quit the chat room") else: msg = "C% s% s"% (name,text) # message type, name, message s.sendto (msg.encode (), ADDR) # Loop received information def recv_msg (s): while True: try: data Addr = s.recvfrom (1024) except KeyboardInterrupt: # parent process prevents user Ctrl+C from exiting exit () if data.decode () = = "EXIT": # when the user exits, there is no need to receive any more messages. EXIT allows the parent process to exit exit () # exit the parent process print (data.decode ()) # create a network connection def main (): s = socket (AF_INET) SOCK_DGRAM) while True: name = input ("Please enter nickname:") # enter name if not name: return msg = "L" + name # send request s.sendto (msg.encode (), ADDR) # wait for reply to data Addr = s.recvfrom (1024) if data.decode () = "OK": print ("you have entered the chat room") break else: # login failed print (data.decode ()) # print the error message on the server side directly # creation process pid = fork () if pid < 0: print (" Error ") elif pid = 0: # the child process sends a message send_msg (s Name) else: # parent process receives message recv_msg (s) main ()
Run the server first, then the client
This is the end of the article on "how to implement a simple chat room with python code". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use python code to achieve a simple chat room". If you want to learn more, you are welcome to 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.