• threadingSocket 服务器端可以同时接受多个客户端的访问


     server.py文件:

    #!/usr/bin/env python
    #coding:utf-8
    import SocketServer
    class  MyServer(SocketServer.BaseRequestHandler):
        def setup(self):
            pass
        def handle(self):
            '''
            print self.request
            print self.client_address
            print self.server
            '''
            conn=self.request
            conn.send('hello!')
            flag=True
            while flag:
                data=conn.recv(1024)
                print data
                if data=='exit':
                    flag=False
                conn.send('shaobing')
            conn.close()
        def finish(self):
            pass
     
    if __name__ == '__main__':
        server = SocketServer.ThreadingTCPServer(('127.0.0.1',9999),MyServer)
        server.serve_forever()
    

    client.py文件:

    #!/usr/bin/env python
    #coding:utf-8
    import socket,os
    ip_port = ('127.0.0.1',9999)
    sk = socket.socket()
    sk.connect(ip_port)
    #container = {'key':'','data':''}
    while True:
        input = raw_input('cmd and path:')#cmd和path之间用'/'分割
        cmd,path = input.split('/') 
        file_name = os.path.basename(path)
        file_size=os.stat(path).st_size
        sk.send(cmd+"/"+file_name+'/'+str(file_size))
        send_size = 0
        f= file(path,'rb')
        Flag = True
        while Flag:
            if send_size + 1024 >=file_size:
                data = f.read(file_size-send_size)
                Flag = False
            else:
                data = f.read(1024)
                send_size+=1024
            sk.send(data)
        f.close()
    sk.close()
    

      

  • 相关阅读:
    同步类容器和并发类容器
    T4模板生成自定义的实体类
    C# UrlEncode 编码
    PLSQL快速生成增删改查语句
    Oracle 检查星期只能是1-7的数字不能重复
    PLSQL 插入数据无响应
    C# DataTable 排序
    C# 获取程序集信息
    C# 调用WinRAR解压缩文件
    DataTable 获取一列最大值并修改
  • 原文地址:https://www.cnblogs.com/noticeable/p/15138172.html
Copyright © 2020-2023  润新知