首页 > 代码库 > python-TCP传输模型

python-TCP传输模型

 1 #!/usr/bin/python
 2 #coding=utf-8
 3 #服务器端
 4 from socket import *
 5 from time import ctime
 6 
 7 HOST="192.168.1.33"
 8 ADDR=1234
 9 t=(HOST,ADDR)
10 
11 sockob=socket(AF_INET,SOCK_STREAM)
12 sockob.bind((HOST,ADDR))
13 sockob.listen(3)
14 print "starting listen....."
15 while 1:
16     a,b=sockob.accept()
17     print "connet from :",b
18     while 1:
19         data=http://www.mamicode.com/a.recv(1024)
20         if not data:
21             break
22         a.send("[%s]:%s"%(ctime(),data))
23 sockob.close()
24 a.close()

 

 1 #!/usr/bin/python
 2 #coding=utf-8
 3 #客户端
 4 from socket import *
 5 import sys
 6 HOST=sys.argv[1]
 7 PORT=int(sys.argv[2])
 8 BUFSIZE=1024
 9 ADDR=(HOST,PORT)
10 sockob=socket(AF_INET,SOCK_STREAM)
11 sockob.connect(ADDR)
12 while 1:
13     data=http://www.mamicode.com/raw_input(">",)
14     if not data:
15         break
16     sockob.send(data)
17     data=http://www.mamicode.com/sockob.recv(BUFSIZE)
18     print data
19 sockob.close()

 

python-TCP传输模型