码迷,mamicode.com
首页 > 编程语言 > 详细

python3 socketserver

时间:2019-05-29 23:51:22      阅读:207      评论:0      收藏:0      [点我收藏+]

标签: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)

 

python3 socketserver

标签:pre   utf-8   data   res   nec   soc   pytho   strip()   elf   

原文地址:https://www.cnblogs.com/lilyxiaoyy/p/10946916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!