Python网络编程基础笔记-Half Open Socket

1.Half-Open服务器,每个Socket只允许进行一次读(接收)操作
# -*- coding: cp936 -*-
"""
半开连接服务器:创建的每个socket只允许进行一次读取操作
"""
import socket
host = ""
port = 50000
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
s.listen(5)
while 1:
    clientsock,clientaddr = s.accept()
    buf = clientsock.recv(8196)
    # 关闭读(接收)操作
    clientsock.shutdown(socket.SHUT_RD)
    if len(buf) != 0:
        print str(clientaddr),buf
    clientsock.close()
   
2.Half-Open客户端,只允许进行一次写(发送)操作
# -*- coding: cp936 -*-
"""
半开连接客户端:创建的这个socket只允许进行写(发送)一次数据
"""
import socket
host = "localhost"
port = 50000
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
s.sendall("message comes from write-only client" + "\r\n")
# 关闭写(发送)操作
s.shutdown(socket.SHUT_WR)
s.close()