• 29 Socketserver和 ftp


    一.Socketserver

    #服务端
    import socketserver
    
    class KnightServer(socketserver.BaseRequestHandler):
    
        def handle(self):
            # self.request  相当于conn链接通道
            while 1:
                from_client_msg = self.request.recv(1024)
                print(from_client_msg.decode('utf-8'))
                server_msg = input('服务端说>>>')
                self.request.send(server_msg.encode('utf-8'))
    
    
    if __name__ == '__main__':
        ip_port = ('127.0.0.1',8001)
        server = socketserver.ThreadingTCPServer(ip_port,KnightServer)
        server.serve_forever()
    
    #客户端
    import socket
    import time
    client = socket.socket()
    client.connect(('127.0.0.1',8001))
    
    while 1:
        client_data = input('别开车>>>')
        client.send(client_data.encode('utf-8'))
    
        from_server_msg = client.recv(1024)
        print(from_server_msg.decode('utf-8'))
    
    client.close()
    socketserver

    #ftp文件上传服务端

    import socket
    import time
    import json
    server = socket.socket()

    server.bind(('127.0.0.1',8001))
    server.listen()

    conn,addr = server.accept()

    def upload(conn,client_file_info):
    #
    upload_file_path = r'D:jj' + '\' + client_file_info['file_name']
    recv_all_datalen = 0
    with open(upload_file_path,'wb') as f:
    while recv_all_datalen < client_file_info['file_size']:
    every_recv_data = conn.recv(1024)
    every_recv_datalen = len(every_recv_data)
    recv_all_datalen += every_recv_datalen
    f.write(every_recv_data)

    print('客户端上传文件')


    client_userinfo = conn.recv(1024).decode('utf-8')
    #定义一个需要返回给服务端的功能列表
    server_function_list = {'1':'上传','2':'下载'}
    #将功能字典转换为json字符串,然后进行编码,转成bytes类型
    json_func_list = json.dumps(server_function_list,ensure_ascii=False)


    #服务端登录认证
    with open('userinformation','r',encoding='utf-8') as f:
    for i in f:
    if i.strip() == client_userinfo:
    #登录成功以后,将服务端能够提供的功能,发送给客户端
    conn.send(json_func_list.encode('utf-8'))
    #接收客户端的文件描述信息,其中有一项是功能序号
    client_fileinfo_json = conn.recv(1024).decode('utf-8')
    client_file_info = json.loads(client_fileinfo_json)

    print(client_file_info)
    if client_file_info['func_choice'] == '1':
    upload(conn,client_file_info)

    break
    else:
    #登录错误的信息码
    conn.send(b'201')

    #ftp 文件上传客户端

    import socket
    import time
    import json
    server = socket.socket()

    server.bind(('127.0.0.1',8001))
    server.listen()

    conn,addr = server.accept()

    def upload(conn,client_file_info):
    #
    upload_file_path = r'D:jj' + '\' + client_file_info['file_name']
    recv_all_datalen = 0
    with open(upload_file_path,'wb') as f:
    while recv_all_datalen < client_file_info['file_size']:
    every_recv_data = conn.recv(1024)
    every_recv_datalen = len(every_recv_data)
    recv_all_datalen += every_recv_datalen
    f.write(every_recv_data)

    print('客户端上传文件')


    client_userinfo = conn.recv(1024).decode('utf-8')
    #定义一个需要返回给服务端的功能列表
    server_function_list = {'1':'上传','2':'下载'}
    #将功能字典转换为json字符串,然后进行编码,转成bytes类型
    json_func_list = json.dumps(server_function_list,ensure_ascii=False)


    #服务端登录认证
    with open('userinformation','r',encoding='utf-8') as f:
    for i in f:
    if i.strip() == client_userinfo:
    #登录成功以后,将服务端能够提供的功能,发送给客户端
    conn.send(json_func_list.encode('utf-8'))
    #接收客户端的文件描述信息,其中有一项是功能序号
    client_fileinfo_json = conn.recv(1024).decode('utf-8')
    client_file_info = json.loads(client_fileinfo_json)

    print(client_file_info)
    if client_file_info['func_choice'] == '1':
    upload(conn,client_file_info)

    break
    else:
    #登录错误的信息码
    conn.send(b'201')

    # 打印进度条

    import time
    for i in range(10):
    str_p = i * '*'
    print(' '+ str_p,end='')
    time.sleep(0.5)

  • 相关阅读:
    ASP抽取数据的执行效率(转)
    gridview中onmouseover的效果
    ASP中类的调用(转)
    PHP 类 的使用[实例讲解]
    常用PHP编辑器介绍
    cookies的存入
    如何为gridview控件里的“删除”列添加一个确认对话框?
    windows下忘记mysql超级管理员root密码的解决办法(转)
    记Visual Studio 2010一次令人崩溃的经历
    sql server 2005系统视图sys.sysobjects和sys.all_objects,sys.objects,三者之间有什么区别?
  • 原文地址:https://www.cnblogs.com/knighterrant/p/10039345.html
Copyright © 2020-2023  润新知