• socket ThreadingTCPServer学习笔记


    文件上传
    #
    服务端 while True: conn,address = sk.accept() conn.sendall(bytes('欢迎你小sb',encoding='utf-8')) str_size = str(conn.recv(1024),encoding='utf-8') int_size = int(str_size) print(int_size) #创建文件 f = open('new.jpg','wb') has_recv = 0 #接受文件 while True: if has_recv == int_size: break else: date = conn.recv(1024) f.write(date) has_recv += len(date) f.close() #客户端 import socket import os obj = socket.socket() obj.connect(('127.1.1.1',9999,)) ret_bytes = obj.recv(1024) print(str(ret_bytes,encoding='utf-8')) #发送文件大小 size = os.stat('tp.jpg').st_size obj.sendall(bytes(str(size),encoding='utf-8')) #发送文件 with open('tp.jpg','rb') as f: for i in f: obj.sendall(i) obj.close()
    ThreadingTCPServer
    服务端
    import socketserver
    class MyServer(socketserver.BaseRequestHandler):
        def handle(self):
            conn = self.request
            conn.sendall(bytes('hello小sb',encoding='utf-8'))
            print(1)
    if __name__ == '__main__':
        server = socketserver.ThreadingTCPServer(('127.1.1.1',9999), MyServer)
        server.serve_forever()
    
    
    客户端
    import socket
    obj = socket.socket()
    obj.connect(('127.1.1.1',9999,))
    ret_bytes = obj.recv(1024)
    print(str(ret_bytes,encoding='utf-8'))
    
    obj.close()
  • 相关阅读:
    Ping 笔记
    android之RadioGroup
    Android之activity中新建控件
    案例:TableLayout表格布局——迷你计算器
    android中5大布局
    Android体系结构及activity生命周期
    Android之ADB指令
    Android之activity初讲
    简单介绍Android应用特色及详解四大组件
    开发Android应用怎么更改LOGO图标
  • 原文地址:https://www.cnblogs.com/koushuige/p/7929412.html
Copyright © 2020-2023  润新知