利用shutdown实现半开放的socket
#server.py import socket s=socket.socket() s.bind(('127.0.0.1',2000)) s.listen(5) while 1: cs,address=s.accept() cs.shutdown(0) print 'got cinnected from',address cs.send('hello I am server,weclome') ra=cs.recv(512) print ra raw_input() cs.close()
报错信息
got cinnected from ('127.0.0.1', 56772)
Traceback (most recent call last):
File "server.py", line 10, in <module>
ra=cs.recv(512)
socket.error: [Errno 10058]
#client.py import socket s=socket.socket() s.connect(('127.0.0.1',2000)) data=s.recv(512) print 'the data received is ',data s.send('hihi I am client') s.close()
运行结果
the data received is
hello I am server,weclome
当shutdown参数为0时,该socket可以发送,但不能接收;与之对应的远端不能发送,可以接收
参数为1时,结果相反,参数为2,不可读写
本人初学,有错请指出!多谢