Skip to content

Commit

Permalink
20190307
Browse files Browse the repository at this point in the history
  • Loading branch information
everydayxy committed Mar 7, 2019
1 parent 2162694 commit c1e2e02
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
Binary file added OnePiece/clients/Henry/magcore_game_flow.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
67 changes: 67 additions & 0 deletions PythonExercise/Tool/file_transfer/file_transfer_client.py
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()
68 changes: 68 additions & 0 deletions PythonExercise/Tool/file_transfer/file_transfer_server.py
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()

0 comments on commit c1e2e02

Please sign in to comment.