forked from MiracleYoung/You-are-Pythonista
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2162694
commit c1e2e02
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import socket | ||
from os.path import getsize | ||
import sys | ||
|
||
class ftp_client(): | ||
def __init__(self,ip_port): | ||
self.ip_port = ip_port | ||
self.client = socket.socket() | ||
self.client.connect(self.ip_port) | ||
def run(self): | ||
while True: | ||
try: | ||
msg = input('ftp_client:>>') | ||
if not msg: | ||
continue | ||
elif msg == 'dir': | ||
self.client.send(msg.encode('utf-8')) | ||
data = self.client.recv(1024) | ||
print('{}'.format(data.decode())) | ||
elif msg.split()[0] == 'get': | ||
self.client.send(msg.encode('utf-8')) | ||
file_size = self.client.recv(1024).decode() | ||
print('remote file size : {}'.format(file_size)) | ||
filename = input('Plz input you prepare to save filename and path: ') | ||
self.client.send('ack'.encode('utf-8')) | ||
recv_size = 0 | ||
with open('{}'.format(filename),'wb') as write_f: | ||
while recv_size < int(file_size): | ||
res = self.client.recv(1024) | ||
write_f.write(res) | ||
recv_size += len(res) | ||
print(type(recv_size),type(file_size),recv_size == int(file_size)) | ||
if recv_size == int(file_size): | ||
print('get done !!! plz find file to {}'.format(filename)) | ||
else: | ||
print('lost some data during trasfer...') | ||
|
||
elif msg.split()[0] == 'put': | ||
self.client.send(msg.encode('utf-8')) | ||
filename = msg.split()[1] | ||
filesize = getsize(filename) | ||
print(filesize) | ||
if self.client.recv(1024).decode() == 'ack': | ||
self.client.send(str(filesize).encode('utf-8')) | ||
if self.client.recv(1024).decode() == 'ack': | ||
remoete_filename = input('Plz input filename and path you prepare to put on the remote server: ') | ||
self.client.send(remoete_filename.encode('utf-8')) | ||
if self.client.recv(1024).decode() == 'ack': | ||
with open('{}'.format(filename),'rb') as read_f: | ||
for line in read_f: | ||
self.client.send(line) | ||
print(self.client.recv(1024).decode()) | ||
|
||
elif msg == 'quit' or msg == 'exit': | ||
sys.exit(0) | ||
|
||
else: | ||
print('Wrong command,plz input agian...') | ||
except KeyboardInterrupt: | ||
self.client.close() | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
ip_port = ('localhost',8888) | ||
f_c = ftp_client(ip_port) | ||
f_c.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import socket | ||
import threading | ||
import os | ||
from os.path import getsize | ||
|
||
|
||
class ftp_server(): | ||
def __init__(self,ip_port): | ||
self.server = socket.socket() | ||
self.server.bind(ip_port) | ||
self.server.listen() | ||
|
||
def tcplink(self,conn, addr): | ||
print('Accept new connection from {} ...'.format(addr)) | ||
while True: | ||
data = conn.recv(1024) | ||
cmd = data.decode() | ||
print('recv', cmd) | ||
if cmd == 'dir': | ||
cmd_result = os.popen(cmd).read() | ||
conn.send(cmd_result.encode('utf-8')) | ||
elif cmd.startswith('put'): | ||
print(cmd) | ||
conn.send('ack'.encode('utf-8')) | ||
filesize = conn.recv(1024).decode() | ||
if filesize: | ||
conn.send('ack'.encode('utf-8')) | ||
remote_filename = conn.recv(1024).decode() | ||
conn.send('ack'.encode('utf-8')) | ||
if remote_filename: | ||
recvsize = 0 | ||
with open('{}'.format(remote_filename), 'wb') as write_f: | ||
while recvsize < int(filesize): | ||
res = conn.recv(1024) | ||
write_f.write(res) | ||
recvsize += len(res) | ||
if recvsize == int(filesize): | ||
conn.send('put done !!! plz find file to {}'.format(remote_filename).encode('utf-8')) | ||
else: | ||
conn.send('lost some data during trasfer...'.encode('utf-8')) | ||
elif cmd.startswith('get'): | ||
filename = cmd.split()[1] | ||
print('filename is {}'.format(filename)) | ||
filesize = getsize(filename) | ||
conn.send(str(filesize).encode('utf-8')) | ||
if conn.recv(1024).decode() == 'ack': | ||
with open(filename,'rb') as read_f: | ||
for line in read_f: | ||
conn.send(line) | ||
|
||
else: | ||
conn.send('Wrong command,plz input agian...'.encode('utf-8')) | ||
def run(self): | ||
while True: | ||
try: | ||
conn, addr = self.server.accept() | ||
t = threading.Thread(target=self.tcplink, args=(conn, addr)) | ||
t.start() | ||
except KeyboardInterrupt: | ||
self.server.close() | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
ip_port = ('localhost',8888) | ||
f_s = ftp_server(ip_port) | ||
f_s.run() | ||
|