ftp_socket
Posted whz_it
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ftp_socket相关的知识,希望对你有一定的参考价值。
ftp_socket_client
import socket,os,hashlib
client = socket.socket()
client.connect(("192.168.171.129",9999))
while True:
cmd = input(">>:").strip()
if len(cmd) == 0:continue
if cmd.startswith("get"):
client.send(cmd.encode())
server_response = client.recv(1024)
print("server response:",server_response)
client.send(b"ready to recv file")
file_total_size = int(server_response.decode())
recived_size= 0
filename = cmd.split()[1]
f = open(filename + "new","wb")
m = hashlib.md5()
while recived_size < file_total_size:
if file_total_size - recived_size > 1024:
size = 1024
else:
size = file_total_size - recived_size
print("lsat size:",size)
data = client.recv(size)
recived_size += len(data)
m.update(data)
f.write(data)
else:
new_file_md5 = m.hexdigest()
print("file recv done",recived_size,file_total_size)
f.close()
server_file_md5 = client.recv(1024)
print("server file md5:",server_file_md5)
print("client file md5:",new_file_md5)
client.close()
ftp_socket_server
import socket,os,hashlib
server = socket.socket()
server.bind(("0.0.0.0",9999))
server.listen(5)
while True:
print("start waiting for calling...")
conn,addr = server.accept()
print("conn:",addr)
while True:
print("waiting for new calling...")
data = conn.recv(1024)
if not data:
print("服务器已断开!")
break
cmd,filename = data.decode().split()
print(filename)
if os.path.isfile(filename):
f = open(filename,"rb")
m = hashlib.md5()
file_size = os.stat(filename).st_size
conn.send(str(file_size).encode())
conn.recv(1024)
for line in f:
m.update(line)
conn.send(line)
print("file md5",m.hexdigest())
conn.send(m.hexdigest().encode())
f.close()
print("send done")
server.close()
以上是关于ftp_socket的主要内容,如果未能解决你的问题,请参考以下文章