測試結果:Nonstop server 不支援 Thread,只能使用 fork 。
Client 程式:
#######################################################
## Demo:Opening a Client-Side Socket for Sending Data
#######################################################
print '#'*60
print '# Demo:Opening a Client-Side Socket for Sending Data'
print '#'*60
import thread
import sys,time,os
from socket import *
def client(id):
serverHost = '192.168.110.93'
serverPort = 12343
def now():
return time.ctime(time.time( ))
message = 'I am ID: %s' %id + '- Time: %s' %(now())
if len(sys.argv) > 1:
serverHost = sys.argv[1]
#Create a socket
sSock = socket(AF_INET, SOCK_STREAM)
try :
#Connect to server
sSock.connect((serverHost, serverPort))
except Exception, e:
sSock.close()
os._exit(0)
#Send messages
sSock.send(message)
data = sSock.recv(1024)
print 'Client received: ', data
sSock.close()
os._exit(0)
def main():
i=0
while True:
newpid = os.fork()
if newpid == 0:client(i)
i=i+1
if i > 10 :break
if __name__ =='__main__':
main()
Server 程式:
import SocketServer,time
myHost=''
myPort=12343
class MyClientHandler(SocketServer.BaseRequestHandler):
def handle(self):
print 'Welcome Clinet:',self.client_address
while True:
data=self.request.recv(1024)
time.sleep(1)
if not data:break
print 'Send Back:',data
self.request.send(data)
self.request.close()
myaddr=(myHost,myPort)
server=SocketServer.ThreadingTCPServer(myaddr,MyClientHandler)
server.serve_forever()
沒有留言:
張貼留言