Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
tất cả các ký tự trong một chuỗi có thể được hiểu như là một biểu hiện điều hành thường xuyên sử dụng này nếu bạn có một chuỗi dài với rất nhiều các ký tự đặc biệt và bạn muốn tránh gõ một rất nhiều những dấu xồ nguợc, hoặc nếu bạn nhận được một chuỗi từ một người sử dụng (đối với | 316 CHAPTER 14 NETWORK PROGRAMMING host socket.gethostname port 1234 s.bind host port fdmap s.fileno s s.listen 5 p select.poll p.register s while True events p.poll for fd event in events if fd in fdmap c addr s.accept print Got connection from addr p.register c fdmap c.fileno c elif event select.POLLIN data fdmap fd .recv 1024 if not data No data -- connection closed print fdmap fd .getpeername disconnected p.unregister fd del fdmap fd else print data You can find more information about select and poll in the Python Library Reference http python.org doc lib module-select.html . Also reading the source code of the standard library modules asyncore and asynchat found in the asyncore.py and asynchat.py files in your Python installation can be enlightening. Twisted Twisted from Twisted Matrix Laboratories http twistedmatrix.com is an event-driven networking framework for Python originally developed for network games but now used by all kinds of network software. In Twisted you implement event handlers much like you would in a GUI toolkit see Chapter 12 . In fact Twisted works quite nicely together with several common GUI toolkits Tk GTK Qt and wxWidgets . In this section I ll cover some of the basic concepts and show you how to do some relatively simple network programming using Twisted. Once you grasp the basic concepts you can check out the Twisted documentation available on the Twisted web site along with quite a bit of other information to do some more serious network programming. Twisted is a very rich framework and supports among other things web servers and clients SSH2 SMTP POP3 IMAP4 AIM ICQ IRC MSN Jabber NNTP DNS and more CHAPTER 14 NETWORK PROGRAMMING 317 Downloading and Installing Twisted Installing Twisted is quite easy. First go to the Twisted Matrix web site http twistedmatrix.com and from there follow one of the download links. If you re using Windows download the Windows installer for your version of Python. If you re using some other system .