defserver(address): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 加入socket配置,重用ip和端口 sock.bind(address) sock.listen(1) print("Run this script in another window with \"-c\" to connect") print("Listening at: ", sock.getsockname()) sc, sockname = sock.accept() # 接受客户端的连接 print("Accepted connection from: ", sockname) sc.shutdown(socket.SHUT_WR) message = b"" whileTrue: more = sc.recv(8192) # 8k ifnot more: # recv 返回 "" print("Received zero bytes - end of file") break print("Received {} bytes".format(len(more))) message += more print("Message:\n") print(message.decode("ascii")) sc.close() sock.close()
defclient(address): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(address) # 只停止在某个方向上的数据传输,而一个方向上的数据传输继续进行 # SHUT_RDWR:关闭读写,即不可以使用send/write/recv/read # SHUT_RD:关闭读,即不可以使用recv/read # SHUT_WR:关闭写,即不可以使用send/write sock.shutdown(socket.SHUT_RD) sock.sendall(b"Beautiful is better than ugly.\n") sock.sendall(b"Explicit is better than implicit.\n") sock.sendall(b"Simple is better than complex.\n") sock.close()
if __name__ == "__main__": parser = ArgumentParser(description="Transmit & receive a data stream") parser.add_argument("hostname", nargs="?", default="127.0.0.1", help="IP address or hostname (default:%(default)s") parser.add_argument("-c", action="store_true", help="run as a client") parser.add_argument("-p", type=int, metavar="port", default=1060, help="TCP port number (default:%(default)s") args = parser.parse_args() function = client if args.c else server function((args.hostname, args.p))
运行一下服务端然后再开一个另外的命令行窗口加上 -c 参数来运行客户端程序,得到下面的输出
1 2 3 4 5 6 7 8 9 10 11
(venv) D:\my_py36\Python-Web\network_data>python streamer.py Run this script in another window with"-c" to connect Listening at: ('127.0.0.1', 1060) Accepted connection from: ('127.0.0.1', 56077) Received 96bytes Received zero bytes - end of file Message:
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex.
defrecvall(sock, length): data = "" whilelen(data) < length: more = sock.recv(length - len(data)) ifnot more: raise EOFError("socket closed {} bytes into a {}-byte message".format(len(data), length)) data += more return data
defserver(address): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(address) sock.listen(1) print("Run this script in another window with \"-c\" to connect") print("Listening at: ", sock.getsockname()) sc, sockname = sock.accept() print("Accepted connection from: ", sockname) sc.shutdown(socket.SHUT_WR) whileTrue: block = get_block(sc) ifnot block: break print("Block says: ", repr(block)) sc.close() sock.close()
defclient(address): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(address) sock.shutdown(socket.SHUT_RD) put_block(sock, b"Beautiful is better than ugly.") put_block(sock, b"Explicit is better than implicit") put_block(sock, b"Simple is better than complex") put_block(sock, b"") sock.close()
if __name__ == "__main__": parser = ArgumentParser(description="Transmit & receive over TCP") parser.add_argument("hostname", nargs="?", default="127.0.0.1", help="IP address or hostname (default:%(default)s") parser.add_argument("-c", action="store_true", help="run as a client") parser.add_argument("-p", type=int, metavar="port", default=1060, help="TCP port number (default:%(default)s")
args = parser.parse_args() function = client if args.c else server function((args.hostname, args.p))
和上面的例子同样的运行方法
1 2 3 4 5 6 7
(venv) D:\my_py36\Python-Web\network_data>python blocks.py Run this script in another window with"-c" to connect Listening at: ('127.0.0.1', 1060) Accepted connection from: ('127.0.0.1', 56069) Block says: b'Beautiful is better than ugly.' Block says: b'Explicit is better than implicit' Block says: b'Simple is better than complex'