python socket server 并发(转)
import SocketServer, time, select, sys
from threading import Thread
COMMAND_HELLO = 1
COMMAND_QUIT = 2
# The SimpleRequestHandler class uses this to parse command lines.
class SimpleCommandProcessor:
def __init__(self):
pass
def process(self, line, request):
"""Process a command"""
args = line.split(' ')
command = args[0].lower()
args = args[1:]
if command == 'hello':
request.send('HELLO TO YOU TO!\n\r')
return COMMAND_HELLO
elif command == 'quit':
request.send('OK, SEE YOU LATER\n\r')
return COMMAND_QUIT
else:
request.send('Unknown command: "%s"\n\r' % command)
# SimpleServer extends the TCPServer, using the threading mix in
# to create a new thread for every request.
class SimpleServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
# This means the main server will not do the equivalent of a
# pthread_join() on the new threads. With this set, Ctrl-C will
# kill the server reliably.
daemon_threads = True
# By setting this we allow the server to re-bind to the address by
# setting SO_REUSEADDR, meaning you don't have to wait for
# timeouts when you kill the server and the sockets don't get
 
相关文档:
server:
import sys
import socket
host = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
while 1:
try:
&n ......
每个模块都有一个名称,在模块中可以通过语句来找出模块的名称。这在一个场合特别有用
——就如前面所提到的,当一个模块被第一次输入的时候,这个模块的主块将被运行。
每个Python模块都有它的__name__,如果它是'__main__',这说明这个模块被用户单独运行,
我们可以进行相应的恰当操作。
#!/usr/bin/pytho ......
python 2.4中datetime有strftime方法而无strptime方法
而python2.5中这两个方法均有,而我的开发环境正好是python 2.5,而运行环境则是python 2.4
开发环境下调试好的程序,在服务器上就不run。查了一下python的官方文档
,斜体写着:New
in version 2.5.
不兼容的代码如下:
Python语言
:
test_strptime.py
res ......
from link http://www.tt010.net/cms/show_article/1057.html
发表评论
Post by : BossAdmin @[2009-12-12 17:11:20] views:115
adodb:我们领导推荐的数据库连接组件
bsddb3:BerkeleyDB的连接组件
Cheetah-1.0:我比较喜欢这个版本的cheetah
cherrypy:一个WEB framework
ctypes:用来调用动态链接库
DBUtils:数 ......