Skip to content

Commit c1e2e02

Browse files
committed
20190307
1 parent 2162694 commit c1e2e02

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed
104 KB
Loading

PythonExercise/Tool/file_transfer/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import socket
2+
from os.path import getsize
3+
import sys
4+
5+
class ftp_client():
6+
def __init__(self,ip_port):
7+
self.ip_port = ip_port
8+
self.client = socket.socket()
9+
self.client.connect(self.ip_port)
10+
def run(self):
11+
while True:
12+
try:
13+
msg = input('ftp_client:>>')
14+
if not msg:
15+
continue
16+
elif msg == 'dir':
17+
self.client.send(msg.encode('utf-8'))
18+
data = self.client.recv(1024)
19+
print('{}'.format(data.decode()))
20+
elif msg.split()[0] == 'get':
21+
self.client.send(msg.encode('utf-8'))
22+
file_size = self.client.recv(1024).decode()
23+
print('remote file size : {}'.format(file_size))
24+
filename = input('Plz input you prepare to save filename and path: ')
25+
self.client.send('ack'.encode('utf-8'))
26+
recv_size = 0
27+
with open('{}'.format(filename),'wb') as write_f:
28+
while recv_size < int(file_size):
29+
res = self.client.recv(1024)
30+
write_f.write(res)
31+
recv_size += len(res)
32+
print(type(recv_size),type(file_size),recv_size == int(file_size))
33+
if recv_size == int(file_size):
34+
print('get done !!! plz find file to {}'.format(filename))
35+
else:
36+
print('lost some data during trasfer...')
37+
38+
elif msg.split()[0] == 'put':
39+
self.client.send(msg.encode('utf-8'))
40+
filename = msg.split()[1]
41+
filesize = getsize(filename)
42+
print(filesize)
43+
if self.client.recv(1024).decode() == 'ack':
44+
self.client.send(str(filesize).encode('utf-8'))
45+
if self.client.recv(1024).decode() == 'ack':
46+
remoete_filename = input('Plz input filename and path you prepare to put on the remote server: ')
47+
self.client.send(remoete_filename.encode('utf-8'))
48+
if self.client.recv(1024).decode() == 'ack':
49+
with open('{}'.format(filename),'rb') as read_f:
50+
for line in read_f:
51+
self.client.send(line)
52+
print(self.client.recv(1024).decode())
53+
54+
elif msg == 'quit' or msg == 'exit':
55+
sys.exit(0)
56+
57+
else:
58+
print('Wrong command,plz input agian...')
59+
except KeyboardInterrupt:
60+
self.client.close()
61+
62+
63+
64+
if __name__ == '__main__':
65+
ip_port = ('localhost',8888)
66+
f_c = ftp_client(ip_port)
67+
f_c.run()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import socket
2+
import threading
3+
import os
4+
from os.path import getsize
5+
6+
7+
class ftp_server():
8+
def __init__(self,ip_port):
9+
self.server = socket.socket()
10+
self.server.bind(ip_port)
11+
self.server.listen()
12+
13+
def tcplink(self,conn, addr):
14+
print('Accept new connection from {} ...'.format(addr))
15+
while True:
16+
data = conn.recv(1024)
17+
cmd = data.decode()
18+
print('recv', cmd)
19+
if cmd == 'dir':
20+
cmd_result = os.popen(cmd).read()
21+
conn.send(cmd_result.encode('utf-8'))
22+
elif cmd.startswith('put'):
23+
print(cmd)
24+
conn.send('ack'.encode('utf-8'))
25+
filesize = conn.recv(1024).decode()
26+
if filesize:
27+
conn.send('ack'.encode('utf-8'))
28+
remote_filename = conn.recv(1024).decode()
29+
conn.send('ack'.encode('utf-8'))
30+
if remote_filename:
31+
recvsize = 0
32+
with open('{}'.format(remote_filename), 'wb') as write_f:
33+
while recvsize < int(filesize):
34+
res = conn.recv(1024)
35+
write_f.write(res)
36+
recvsize += len(res)
37+
if recvsize == int(filesize):
38+
conn.send('put done !!! plz find file to {}'.format(remote_filename).encode('utf-8'))
39+
else:
40+
conn.send('lost some data during trasfer...'.encode('utf-8'))
41+
elif cmd.startswith('get'):
42+
filename = cmd.split()[1]
43+
print('filename is {}'.format(filename))
44+
filesize = getsize(filename)
45+
conn.send(str(filesize).encode('utf-8'))
46+
if conn.recv(1024).decode() == 'ack':
47+
with open(filename,'rb') as read_f:
48+
for line in read_f:
49+
conn.send(line)
50+
51+
else:
52+
conn.send('Wrong command,plz input agian...'.encode('utf-8'))
53+
def run(self):
54+
while True:
55+
try:
56+
conn, addr = self.server.accept()
57+
t = threading.Thread(target=self.tcplink, args=(conn, addr))
58+
t.start()
59+
except KeyboardInterrupt:
60+
self.server.close()
61+
62+
63+
64+
if __name__ == '__main__':
65+
ip_port = ('localhost',8888)
66+
f_s = ftp_server(ip_port)
67+
f_s.run()
68+

0 commit comments

Comments
 (0)