zoukankan      html  css  js  c++  java
  • Python_FTP通讯软件

    ftpServer.py

     1 import socket
     2 import threading
     3 import os
     4 import struct
     5 
     6 #用户账号、密码、主目录
     7 #也可以把这些信息存放到数据库中
     8 users = {'zhangsan':{'pwd':'zhangsan1234', 'home':r'c:python 3.5'},
     9          'lisi':{'pwd':'lisi567', 'home':'c:\'}}
    10 
    11 def server(conn,addr, home):
    12     print('新客户端:'+str(addr))
    13     #进入当前用户主目录
    14     os.chdir(home)
    15     while True:
    16         data = conn.recv(100).decode().lower()
    17         #显示客户端输入的每一条命令
    18         print(data)
    19         #客户端退出
    20         if data in ('quit', 'q'):
    21             break
    22         #查看当前文件夹的文件列表
    23         elif data in ('list', 'ls', 'dir'):
    24             files = str(os.listdir(os.getcwd()))
    25             files = files.encode()
    26             conn.send(struct.pack('I', len(files)))
    27             conn.send(files)
    28         #切换至上一级目录
    29         elif ''.join(data.split()) == 'cd..':
    30             cwd = os.getcwd()
    31             newCwd = cwd[:cwd.rindex('\')]
    32             #考虑根目录的情况
    33             if newCwd[-1] == ':':
    34                 newCwd += '\'
    35             #限定用户主目录
    36             if newCwd.lower().startswith(home):
    37                 os.chdir(newCwd)
    38                 conn.send(b'ok')
    39             else:
    40                 conn.send(b'error')
    41         #查看当前目录
    42         elif data in ('cwd', 'cd'):
    43             conn.send(str(os.getcwd()).encode())
    44         elif data.startswith('cd '):
    45             #指定最大分隔次数,考虑目标文件夹带有空格的情况
    46             #只允许使用相对路径进行跳转
    47             data = data.split(maxsplit=1)
    48             if len(data) == 2 and  os.path.isdir(data[1]) 
    49                and data[1]!=os.path.abspath(data[1]):
    50                 os.chdir(data[1])
    51                 conn.send(b'ok')
    52             else:
    53                 conn.send(b'error')
    54         #下载文件
    55         elif data.startswith('get '):
    56             data = data.split(maxsplit=1)
    57             #检查文件是否存在
    58             if len(data) == 2 and os.path.isfile(data[1]):
    59                 conn.send(b'ok')
    60                 fp = open(data[1], 'rb')
    61                 while True:
    62                     content = fp.read(4096)
    63                     #发送文件结束
    64                     if not content:
    65                         conn.send(b'overxxxx')
    66                         break
    67                     #发送文件内容
    68                     conn.send(content)
    69                     if conn.recv(10) == b'ok':
    70                         continue
    71                 fp.close()
    72             else:
    73                 conn.send(b'no')
    74         #无效命令
    75         else:
    76             pass
    77             
    78     conn.close()
    79     print(str(addr)+'关闭连接')
    80 
    81 #创建Socket,监听本地端口,等待客户端连接
    82 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    83 sock.bind(('', 10600))
    84 sock.listen(5)
    85 while True:
    86     conn, addr = sock.accept()
    87     #验证客户端输入的用户名和密码是否正确
    88     userId, userPwd = conn.recv(1024).decode().split(',')
    89     if userId in users and users[userId]['pwd'] == userPwd:
    90         conn.send(b'ok')
    91         #为每个客户端连接创建并启动一个线程,参数为连接、客户端地址、客户主目录
    92         home = users[userId]['home']
    93         t = threading.Thread(target=server, args=(conn,addr,home))
    94         t.daemon = True
    95         t.start()
    96     else:
    97         conn.send(b'error')

    ftpClient.py

     1 import socket
     2 import threading
     3 import os
     4 import struct
     5 
     6 #用户账号、密码、主目录
     7 #也可以把这些信息存放到数据库中
     8 users = {'zhangsan':{'pwd':'zhangsan1234', 'home':r'c:python 3.5'},
     9          'lisi':{'pwd':'lisi567', 'home':'c:\'}}
    10 
    11 def server(conn,addr, home):
    12     print('新客户端:'+str(addr))
    13     #进入当前用户主目录
    14     os.chdir(home)
    15     while True:
    16         data = conn.recv(100).decode().lower()
    17         #显示客户端输入的每一条命令
    18         print(data)
    19         #客户端退出
    20         if data in ('quit', 'q'):
    21             break
    22         #查看当前文件夹的文件列表
    23         elif data in ('list', 'ls', 'dir'):
    24             files = str(os.listdir(os.getcwd()))
    25             files = files.encode()
    26             conn.send(struct.pack('I', len(files)))
    27             conn.send(files)
    28         #切换至上一级目录
    29         elif ''.join(data.split()) == 'cd..':
    30             cwd = os.getcwd()
    31             newCwd = cwd[:cwd.rindex('\')]
    32             #考虑根目录的情况
    33             if newCwd[-1] == ':':
    34                 newCwd += '\'
    35             #限定用户主目录
    36             if newCwd.lower().startswith(home):
    37                 os.chdir(newCwd)
    38                 conn.send(b'ok')
    39             else:
    40                 conn.send(b'error')
    41         #查看当前目录
    42         elif data in ('cwd', 'cd'):
    43             conn.send(str(os.getcwd()).encode())
    44         elif data.startswith('cd '):
    45             #指定最大分隔次数,考虑目标文件夹带有空格的情况
    46             #只允许使用相对路径进行跳转
    47             data = data.split(maxsplit=1)
    48             if len(data) == 2 and  os.path.isdir(data[1]) 
    49                and data[1]!=os.path.abspath(data[1]):
    50                 os.chdir(data[1])
    51                 conn.send(b'ok')
    52             else:
    53                 conn.send(b'error')
    54         #下载文件
    55         elif data.startswith('get '):
    56             data = data.split(maxsplit=1)
    57             #检查文件是否存在
    58             if len(data) == 2 and os.path.isfile(data[1]):
    59                 conn.send(b'ok')
    60                 fp = open(data[1], 'rb')
    61                 while True:
    62                     content = fp.read(4096)
    63                     #发送文件结束
    64                     if not content:
    65                         conn.send(b'overxxxx')
    66                         break
    67                     #发送文件内容
    68                     conn.send(content)
    69                     if conn.recv(10) == b'ok':
    70                         continue
    71                 fp.close()
    72             else:
    73                 conn.send(b'no')
    74         #无效命令
    75         else:
    76             pass
    77             
    78     conn.close()
    79     print(str(addr)+'关闭连接')
    80 
    81 #创建Socket,监听本地端口,等待客户端连接
    82 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    83 sock.bind(('', 10600))
    84 sock.listen(5)
    85 while True:
    86     conn, addr = sock.accept()
    87     #验证客户端输入的用户名和密码是否正确
    88     userId, userPwd = conn.recv(1024).decode().split(',')
    89     if userId in users and users[userId]['pwd'] == userPwd:
    90         conn.send(b'ok')
    91         #为每个客户端连接创建并启动一个线程,参数为连接、客户端地址、客户主目录
    92         home = users[userId]['home']
    93         t = threading.Thread(target=server, args=(conn,addr,home))
    94         t.daemon = True
    95         t.start()
    96     else:
    97         conn.send(b'error')
  • 相关阅读:
    GridView 应用貌似是个mm写的,值得尊敬!
    .net 时间函数
    .net 获取url的方法
    SaveGraphics
    asp网站页面上都是问号
    由于编码方式导致CSS样式表失效
    .net url乱码
    常用正则表达式
    解决realse版在加载toolbar后不正常退出的现象
    general error c1010070: Failed to load and parse the manifest
  • 原文地址:https://www.cnblogs.com/cmnz/p/7092275.html
Copyright © 2011-2022 走看看