Python网络通信之socket模块(四)基于Tcp/Ip的TCP交互通信serve/client的编写过程
Server_v1:缺陷是服务端只能接收一次请求就关闭链接了[root@ChangerLee 模拟Http服务器返回状态]# cat server_test_v1.py !/bin/env python#import socketsk = socket.socket()ip_port = ('127.0.0.1',9999)sk.bind(ip_port)sk.listen(5)clisoc
·
Server_v1:
缺陷是服务端只能接收一次请求就关闭链接了
[root@ChangerLee 模拟Http服务器返回状态]# cat server_test_v1.py
!/bin/env python
#import socket
sk = socket.socket()
ip_port = ('127.0.0.1',9999)
sk.bind(ip_port)
sk.listen(5)
clisock,clieaddr = sk.accept()
clisock.send('hello')
clisock.close()
[root@ChangerLee 模拟Http服务器返回状态]# python server_test_v1.py &
[1] 11057
[root@ChangerLee 模拟Http服务器返回状态]# netstat -antple|grep "9999"
tcp 0 0 127.0.0.1:9999 0.0.0.0:* LISTEN 0 88903 11057/python
Client_v1
[root@ChangerLee 模拟Http服务器返回状态]# cat client_test_v1.py
#!/bin/env python
import socket
clisock = socket.socket()
ip_port = ('127.0.0.1',9999)
clisock.connect(ip_port)
data = clisock.recv(1024)
print data
[root@ChangerLee 模拟Http服务器返回状态]# python client_test_v1.py
hello
Server_v2:
多次建立TCP链接,进行通信
[root@ChangerLee 模拟Http服务器返回状态]# cat server_test_v2.py
#!/bin/env python
import socket
sk = socket.socket()
ip_port = ('127.0.0.1',9999)
sk.bind(ip_port)
sk.listen(5)
while True:
clisock,clieaddr = sk.accept()
clisock.send('hello')
clisock.close()
[root@ChangerLee 模拟Http服务器返回状态]# python server_test_v2.py
Client_v2
[root@ChangerLee 模拟Http服务器返回状态]# cat client_test_v2.py
#!/bin/env python
import socket
clisock = socket.socket()
ip_port = ('127.0.0.1',9999)
clisock.connect(ip_port)
data = clisock.recv(1024)
print data
[root@ChangerLee 模拟Http服务器返回状态]# python client_test_v2.py
hello
[root@ChangerLee 模拟Http服务器返回状态]# python client_test_v2.py
hello
[root@ChangerLee 模拟Http服务器返回状态]# python client_test_v2.py
hello
Server_v3:
套接字属性查看
[root@ChangerLee 模拟Http服务器返回状态]# cat server_test_v1.py
#!/bin/env python
import socket
sk = socket.socket()
ip_port = ('127.0.0.1',9999)
sk.bind(ip_port)
sk.listen(5)
while True:
clisock,cliaddr = sk.accept()
print (sk)
print (clisock)
print (cliaddr)
clisock.send('hello')
clisock.close()
[root@ChangerLee 模拟Http服务器返回状态]# python server_test_v3.py
<socket._socketobject object at 0x7f7fe9c533d0>
<socket._socketobject object at 0x7f7fe9c53440>
('127.0.0.1', 43454)
其时sk.accept()返回的是一个元组类型(<socket._socketobject object at 0x7f7fe9c53440>
,('127.0.0.1', 43454))
Server_v4:
该c/s对话通信:缺陷再与客户端不能直接退出
[root@ChangerLee 模拟Http服务器返回状态]# cat server_test_v4.py
#!/bin/env python
#coding=utf-8
import socket
sk = socket.socket()
ip_port = ('127.0.0.1',9999)
sk.bind(ip_port)
sk.listen(5)
while True:
clisock,cliaddr = sk.accept()
clisock.send('hello')
flag = True
while flag:
#不直接用break的原因是在多层循环的中break跳出循环会出现问题
data = clisock.recv(1024)
#接收方法recv也是阻塞的
print ('from client:'+data)
if data == 'exit':
flag = False
clisock.send('sb')
clisock.close()
[root@ChangerLee 模拟Http服务器返回状态]# python server_test_v4.py
from client:hello
from client:exit
直接卡住。。。。。。
Client_v4
[root@ChangerLee 模拟Http服务器返回状态]# cat client_test_v4.py
#!/bin/env python
import socket
clisock = socket.socket()
ip_port = ('127.0.0.1',9999)
clisock.connect(ip_port)
while True:
data = clisock.recv(1024)
inp = raw_input('client>')
clisock.send(inp)
print ('getinfo:',data)
[root@ChangerLee 模拟Http服务器返回状态]# python client_test_v4.py
client>hello
('getinfo:', 'hello')
client>exit
('getinfo:', 'sb')
client>exit
('getinfo:', 'sb')
client>exit
Traceback (most recent call last):
File "client_test_v2.py", line 11, in <module>
clisock.send(inp)
socket.error: [Errno 32] Broken pipe
socket.error: [Errno 32] Broken pipe往往是客户端异常断开造成的
Server_v5:
[root@ChangerLee 模拟Http服务器返回状态]# cat server_test_v5.py
#!/bin/env python
#coding=utf-8
import socket
sk = socket.socket()
ip_port = ('127.0.0.1',9999)
sk.bind(ip_port)
sk.listen(5)
while True:
clisock,cliaddr = sk.accept()
clisock.send('hello')
flag = True
while flag:
data = clisock.recv(1024)
print ('from client:'+data)
if data == 'exit':
flag = False
clisock.send('sb')
clisock.close()
[root@ChangerLee 模拟Http服务器返回状态]# python server_test_v5.py
from client:hello
from client:exit
from client:come again
from client:exit
Client_v5:
最终版本
[root@ChangerLee 模拟Http服务器返回状态]# cat client_test_v5.py
#!/bin/env python
import socket
clisock = socket.socket()
ip_port = ('127.0.0.1',9999)
clisock.connect(ip_port)
while True:
data = clisock.recv(1024)
inp = raw_input('client>')
clisock.send(inp)
print ('getinfo:',data)
if inp == 'exit':
break
[root@ChangerLee 模拟Http服务器返回状态]# python client_test_v5.py
client>hello
('getinfo:', 'hello')
client>exit
('getinfo:', 'sb')
[root@ChangerLee 模拟Http服务器返回状态]# python client_test_v5.py
client>come again
('getinfo:', 'hello')
client>exit
('getinfo:', 'sb')
更多推荐
已为社区贡献2条内容
所有评论(0)