# coding: utf-8
import socketserver
class MyTCPHandler(socketserver.BaseRequestHandler):
"""
The request handler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print("{} wrote:".format(self.client_address[0]))
print(self.data)
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
# Create the server, binding to localhost on port 9999
# 如果子类没有某个方法或是属性, 就回去父类中调用
with socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) as server:
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
# coding: utf-8
import socket
sk = socket.socket()
sk.connect(("127.0.0.1", 9999)) # 主动初始化与服务器端的连接
while True:
send_data = input("输入发送内容:")
sk.sendall(bytes(send_data, encoding="utf8"))
if send_data == "byebye":
break
accept_data = str(sk.recv(1024), encoding="utf8")
print("".join(("接收内容:", accept_data)))
sk.close()
class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass
with socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) as server: # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server.serve_forever()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有