标签:pre utf-8 data res nec soc pytho strip() elf
-------------------------tcp_server.py-------------------------
import socketserver
class Myserver(socketserver.BaseRequestHandler):
def handle(self):
self.data = self.request.recv(1024).strip() # self.request相当于conn
print(self.client_address[0], self.data)
self.request.sendall(self.data.upper())
if __name__ == ‘__main__‘:
ip_port = ("127.0.0.1", 8001)
socketserver.TCPServer.allow_reuse_address = True
server = socketserver.ThreadingTCPServer(ip_port, Myserver)
server.serve_forever()
-------------------------tcp_client.py-------------------------
# coding:utf-8
import socket
ip_port = ("127.0.0.1", 8001)
data = "hello world"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(ip_port)
sock.sendall(data.encode("utf-8"))
from_server_msg = sock.recv(1024).decode("utf-8")
print("Send: ", data)
print("Received: ", from_server_msg)
标签:pre utf-8 data res nec soc pytho strip() elf
原文地址:https://www.cnblogs.com/lilyxiaoyy/p/10946916.html