代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
多线程Socket服务器模块
作者:
Dezai.CN
/ 发布于
2013/12/3
/
1444
#!/usr/bin/env python # -*- coding: utf8 -*- # # [SNIPPET_NAME: Threaded Server] # [SNIPPET_CATEGORIES: Python Core, socket, threading] # [SNIPPET_DESCRIPTION: Simple example of Python's socket and threading modules] # [SNIPPET_DOCS: http://docs.python.org/library/socket.html, http://docs.python.org/library/threading.html] # [SNIPPET_AUTHOR: Gonzalo <gnunezr@gmail.com>] # [SNIPPET_LICENSE: GPL] import sys import socket import threading import time QUIT = False class ClientThread( threading.Thread ): ''' Class that implements the client threads in this server ''' def __init__( self, client_sock ): ''' Initialize the object, save the socket that this thread will use. ''' threading.Thread.__init__( self ) self.client = client_sock def run( self ): ''' Thread's main loop. Once this function returns, the thread is finished and dies. ''' # # Need to declare QUIT as global, since the method can change it # global QUIT done = False cmd = self.readline() # # Read data from the socket and process it # while not done: if 'quit' == cmd : self.writeline( 'Ok, bye' ) QUIT = True done = True elif 'bye' == cmd: self.writeline( 'Ok, bye' ) done = True else: self.writeline( self.name ) cmd = self.readline() # # Make sure the socket is closed once we're done with it # self.client.close() return def readline( self ): ''' Helper function, reads up to 1024 chars from the socket, and returns them as a string, all letters in lowercase, and without any end of line markers ''' result = self.client.recv( 1024 ) if( None != result ): result = result.strip().lower() return result def writeline( self, text ): ''' Helper function, writes teh given string to the socket, with an end of line marker appended at the end ''' self.client.send( text.strip() + '\n' ) class Server: ''' Server class. Opens up a socket and listens for incoming connections. Every time a new connection arrives, it creates a new ClientThread thread object and defers the processing of the connection to it. ''' def __init__( self ): self.sock = None self.thread_list = [] def run( self ): ''' Server main loop. Creates the server (incoming) socket, and listens on it of incoming connections. Once an incomming connection is deteceted, creates a ClientThread to handle it, and goes back to listening mode. ''' all_good = False try_count = 0 # # Attempt to open the socket # while not all_good: if 3 < try_count: # # Tried more than 3 times, without success... Maybe the port # is in use by another program # sys.exit( 1 ) try: # # Create the socket # self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) # # Bind it to the interface and port we want to listen on # self.sock.bind( ( '127.0.0.1', 5050 ) ) # # Listen for incoming connections. This server can handle up to # 5 simultaneous connections # self.sock.listen( 5 ) all_good = True break except socket.error, err: # # Could not bind on the interface and port, wait for 10 seconds # print 'Socket connection error... Waiting 10 seconds to retry.' del self.sock time.sleep( 10 ) try_count += 1 print "Server is listening for incoming connections." print "Try to connect through the command line, with:" print "telnet localhost 5050" print "and then type whatever you want." print print "typing 'bye' finishes the thread, but not the server ", print "(eg. you can quit telnet, run it again and get a different ", print "thread name" print "typing 'quit' finishes the server" try: # # NOTE - No need to declare QUIT as global, since the method never # changes its value # while not QUIT: try: # # Wait for half a second for incoming connections # self.sock.settimeout( 0.500 ) client = self.sock.accept()[0] except socket.timeout: # # No connection detected, sleep for one second, then check # if the global QUIT flag has been set # time.sleep( 1 ) if QUIT: print "Received quit command. Shutting down..." break continue # # Create the ClientThread object and let it handle the incoming # connection # new_thread = ClientThread( client ) print 'Incoming Connection. Started thread ', print new_thread.getName() self.thread_list.append( new_thread ) new_thread.start() # # Go over the list of threads, remove those that have finished # (their run method has finished running) and wait for them # to fully finish # for thread in self.thread_list: if not thread.isAlive(): self.thread_list.remove( thread ) thread.join() except KeyboardInterrupt: print 'Ctrl+C pressed... Shutting Down' except Exception, err: print 'Exception caught: %s\nClosing...' % err # # Clear the list of threads, giving each thread 1 second to finish # NOTE: There is no guarantee that the thread has finished in the # given time. You should always check if the thread isAlive() after # calling join() with a timeout paramenter to detect if the thread # did finish in the requested time # for thread in self.thread_list: thread.join( 1.0 ) # # Close the socket once we're done with it # self.sock.close() if "__main__" == __name__: server = Server() server.run() print "Terminated"
试试其它关键字
多线程
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3