码迷,mamicode.com
首页 > 编程语言 > 详细

Python_FTP通讯软件

时间:2017-06-29 09:53:18      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:target   files   bre   threading   getc   and   xxx   mon   pat   

ftpServer.py

 1 import socket
 2 import threading
 3 import os
 4 import struct
 5 
 6 #用户账号、密码、主目录
 7 #也可以把这些信息存放到数据库中
 8 users = {zhangsan:{pwd:zhangsan1234, home:rc:\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(bok)
39             else:
40                 conn.send(berror)
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(bok)
52             else:
53                 conn.send(berror)
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(bok)
60                 fp = open(data[1], rb)
61                 while True:
62                     content = fp.read(4096)
63                     #发送文件结束
64                     if not content:
65                         conn.send(boverxxxx)
66                         break
67                     #发送文件内容
68                     conn.send(content)
69                     if conn.recv(10) == bok:
70                         continue
71                 fp.close()
72             else:
73                 conn.send(bno)
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(bok)
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(berror)

ftpClient.py

 1 import socket
 2 import threading
 3 import os
 4 import struct
 5 
 6 #用户账号、密码、主目录
 7 #也可以把这些信息存放到数据库中
 8 users = {zhangsan:{pwd:zhangsan1234, home:rc:\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(bok)
39             else:
40                 conn.send(berror)
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(bok)
52             else:
53                 conn.send(berror)
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(bok)
60                 fp = open(data[1], rb)
61                 while True:
62                     content = fp.read(4096)
63                     #发送文件结束
64                     if not content:
65                         conn.send(boverxxxx)
66                         break
67                     #发送文件内容
68                     conn.send(content)
69                     if conn.recv(10) == bok:
70                         continue
71                 fp.close()
72             else:
73                 conn.send(bno)
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(bok)
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(berror)

 

Python_FTP通讯软件

标签:target   files   bre   threading   getc   and   xxx   mon   pat   

原文地址:http://www.cnblogs.com/cmnz/p/7092275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!