Created
January 16, 2013 11:15
-
-
Save chenzx/4546421 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: GBK -*- | |
#----------------------------------------------------------- | |
#TCP 数据服务器,当客户端连接上时,不断写入数据 | |
# 用于测试TCP连接吞吐量 | |
import os,sys,time | |
from socket import socket, AF_INET, AF_INET6, SOCK_STREAM | |
#----------------------------------------------------------- | |
#常量定义: | |
PROGRAM = 'TCP Data Server' | |
PACKET_SIZE = 1024 # | |
#----------------------------------------------------------- | |
#辅助函数定义: | |
def deal_with_client( s_client ): | |
while 1: | |
now = '%10.6f' % ( time.time() ) | |
test_data = now.ljust(PACKET_SIZE,'.') | |
try: | |
s_client.send( test_data ) | |
except Exception,e: | |
print 'client abort: ' , e | |
raise e | |
#终止这个客户端; | |
#----------------------------------------------------------- | |
#解析命令行参数: | |
def usage(): | |
print PROGRAM | |
print 'Usage: ',sys.argv[0], ' 4/6 [listen_port]' | |
sys.exit(0) | |
def parseCmdArgs(): | |
import sys,string | |
#print 'arguments: ',sys.argv[1:] | |
if len(sys.argv) == 3: | |
#Reset CONFIG_IPV6 & LISTEN_PORT: | |
if sys.argv[1] == '4': | |
CONFIG_IPV6 = 0 | |
elif sys.argv[1] == '6': | |
CONFIG_IPV6 = 1 | |
else: | |
print 'error protocol %s, MUST be 4/6' % sys.argv[1] | |
usage() | |
# | |
try: | |
LISTEN_PORT = string.atoi( sys.argv[2] ) | |
except ValueError: | |
print 'error port %s, NOT a number' % sys.argv[2] | |
usage() | |
return (CONFIG_IPV6,LISTEN_PORT) | |
else: | |
usage() | |
#----------------------------------------------------------- | |
#建立TCP服务器Socket: | |
def serverSocket(ipv6,port): | |
AF_FAMILY = AF_INET | |
if ipv6 == 1: AF_FAMILY = AF_INET6 | |
S = socket( AF_FAMILY, SOCK_STREAM ) | |
try: | |
S.bind(('', port)) | |
except Exception: | |
print "bind error %s: %s \n %s" % sys.exc_info() | |
sys.exit(-1) | |
else: | |
return S | |
#----------------------------------------------------------- | |
#Main: | |
if __name__ == '__main__': | |
#解析命令行参数: | |
(CONFIG_IPV6,LISTEN_PORT) = parseCmdArgs() | |
#输出程序启动信息: | |
print 'Start TCP Data Server ...' | |
print 'Config IPv6 = %d' % CONFIG_IPV6 | |
print 'Listen Port = %d' % LISTEN_PORT | |
S = serverSocket(CONFIG_IPV6,LISTEN_PORT) | |
S.listen( 5 ); | |
print 'listening...' | |
clients_num = 0 | |
while 1: #clients_num<MAX_CLIENTS: | |
try: | |
s_client,info = S.accept() | |
print 'new tcp client connected:',info | |
clients_num += 1 | |
#派生新线程处理此客户端连接: | |
import threading | |
T = threading.Thread(target=deal_with_client, args=[s_client]) | |
T.start() | |
finally: | |
#s_client.close() | |
print 'new thread started.' | |
print 'exit TCP Data Server.' | |
sys.exit(0) | |
#never reach; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment