python virtual tea party effect
This time I will bring you the effect of the python virtual tea party. What are the precautions for the effect of the python virtual tea party? The following is a practical case, let’s take a look.
When learning and using almost any The overall idea of this exercise is to first have a chat server. The main functions of this server are to provide client socket connections, store the connection session of each client, process the messages sent by each connection, and parse the client. sent data. That's it. As for the client, you don't need to write code, just use the system's telnet tool. I think with the above analysis, there is nothing much to say about the rest of this program, except of course the two classes that encapsulate sockets. I tried this by using the socket class in python and wrote a simple communication program. However, for some reason, accidents always occurred during communication. This simple code is as follows: server.pyimport socket mysocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) mysocket.bind(('',8888)) mysocket.listen(5) while True: connection,addr = mysocket.accept() revStr = connection.recv(1024) connection.send('Server:' + revStr) connection.close()
import socket import time clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) clientsocket.connect(('',8888)) while True: time.sleep(2) clientsocket.send('hello the5fire') print clientsocket.recv(1024) clientsocket.close()
from asyncore import dispatcher from asynchat import async_chat import socket, asyncore PORT = 5005 NAME = 'TestChat' class EndSession(Exception):pass class CommandHandler: def unknown(self, session, cmd): session.push('Unknown command: %s\r\n' % cmd) def handle(self, session, line): if not line.strip(): return parts = line.split(' ',1) cmd = parts[0] try: line = parts[1].strip() except IndexError: line = '' meth = getattr(self, 'do_'+cmd, None) try: meth(session, line) except TypeError: self.unknown(session,cmd) class Room(CommandHandler): def init(self, server): self.server = server self.sessions = [] def add(self, session): self.sessions.append(session) def remove(self, session): self.sessions.remove(session) def broadcast(self, line): for session in self.sessions: session.push(line) def do_logout(self, session, line): raise EndSession class LoginRoom(Room): def add(self,session): Room.add(self,session) self.broadcast('Welcome to %s\r\n' % self.server.name) def unknown(self, session, cmd): session.push('Please log in \nUse "login"\r\n') def do_login(self, session, line): name = line.strip() if not name: session.push('Please enter a name\r\n') elif name in self.server.users: session.push('The name "%s" is taken.\r\n' % name) sessoin.push('Please try again.\r\n') else: session.name = name session.enter(self.server.main_room) class ChatRoom(Room): def add(self, session): self.broadcast(session.name + ' has entered the room.\r\n') self.server.users[session.name] = session Room.add(self, session) def remove(self, session): Room.remove(self, session) self.broadcast(session.name + ' has left the room.\r\n') def do_say(self, session, line): self.broadcast(session.name + ': ' + line + '\r\n') def do_look(self, session, line): session.push('The following are in this room:\r\n') for other in self.sessions: session.push(other.name + '\r\n') def do_who(self, session, line): session.push('The following are logged in:\r\n') for name in self.server.users: session.push(name + '\r\n') class LogoutRoom(Room): def add(self, session): try: del self.server.users[session.name] except KeyError: pass class ChatSession(async_chat): def init(self, server, sock): async_chat.init(self,sock) self.server = server self.set_terminator('\r\n') self.data = [] self.name = None self.enter(LoginRoom(server)) def enter(self, room): try: cur = self.room except AttributeError: pass else: cur.remove(self) self.room = room room.add(self) def collect_incoming_data(self, data): self.data.append(data) def found_terminator(self): line = ''.join(self.data) self.data = [] try: self.room.handle(self, line) except EndSession: self.handle_close() def handle_close(self): async_chat.handle_close(self) self.enter(LogoutRoom(self.server)) class ChatServer(dispatcher): def init(self, port, name): dispatcher.init(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.bind(('',port)) self.listen(5) self.name = name self.users = {} self.main_room = ChatRoom(self) def handle_accept(self): conn, addr = self.accept() ChatSession(self,conn) if name == 'main': s = ChatServer(PORT, NAME) try: asyncore.loop() except KeyboardInterrupt: print
strings and commands sent by the client, and they are all inherited from CommandHandler.
Final screenshot:Perfect solution to python2.7 being unable to use pip
How to read txt files into DataFrame in batches with python Format
The above is the detailed content of python virtual tea party effect. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

VS Code not only can run Python, but also provides powerful functions, including: automatically identifying Python files after installing Python extensions, providing functions such as code completion, syntax highlighting, and debugging. Relying on the installed Python environment, extensions act as bridge connection editing and Python environment. The debugging functions include setting breakpoints, step-by-step debugging, viewing variable values, and improving debugging efficiency. The integrated terminal supports running complex commands such as unit testing and package management. Supports extended configuration and enhances features such as code formatting, analysis and version control.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
