今天修改了一个 socket 的例子, 如果结果如下:
#!/usr/bin/env python
#coding=GBK
import SocketServer
import time
import random
class SocketServer_test( SocketServer.StreamRequestHandler ):
def handle( self ):
poem1 = """\n\r床前明月光\n\r疑似地上霜\n\r举头望明月\n\r低头思故乡\n\r\t--李白\n\r"""
poem2 = '\n\rLife is dear, love is dearer. \n\rBoth can be given up for freedom. \n'
if random.random() > 0.5:
poemx = poem1
else:
poemx = poem2
for a in poemx:
self.wfile.write('%s'%(a))
if a in (' ', ',', '.'):
time.sleep(0.5)
elif a == '\n':
time.sleep(1)
else:
time.sleep(0.2)
time.sleep(1)
server = SocketServer.TCPServer( ("", 4321), SocketServer_test )
server.serve_forever()
|
运行结果如下:
阅读(1558) | 评论(0) | 转发(0) |