[英]Sending files to client from server via socket/ftp python
I am new to python.我是python的新手。 Using Ubuntu 18.04, python 3.6.
使用 Ubuntu 18.04,python 3.6。
Tried writing a script to send ANY File from server to client (currently trying to send .pdf file) and vice versa.尝试编写脚本以将任何文件从服务器发送到客户端(当前尝试发送 .pdf 文件),反之亦然。
Earlier i used sockets to send file, but was facing problems with extensions (like sending .pdf file but received .txt file - could not figure out why this was happening).早些时候我使用套接字发送文件,但遇到扩展问题(如发送 .pdf 文件但收到 .txt 文件 - 无法弄清楚为什么会发生这种情况)。 Then used ftp, but stuck at one point right now.
然后使用ftp,但现在卡在某一点。
SERVER SCRIPT:服务器脚本:
import socket
import os
s = socket.socket()
host = ""
port = 9999
s.bind((host, port))
s.listen(5)
print("Binding Done\n")
socket_object, address = s.accept()
print("Connection Established\n")
print("Sending file...")
f = open("cds.pdf", 'rb')
while f.read(1024):
socket_object.send(f.read(1024))
print("Files Send")
f.close()
socket_object.close()
s.close()
CLIENT SCRIPT:客户端脚本:
import socket
import os
from ftplib import FTP
ftp = FTP()
s = socket.socket()
host = "192.168.43.16"
port = 9999
s.connect((host, port))
#ftp.connect(host = "192.168.43.16", port = 9999)
print("Receiving data...")
f = open("cds_copy.pdf", 'wb')
while True:
ftp.retrbinary('RETR cds_copy.pdf', f.write, 1024)
ftp.storbinary('STOR cds_copy.pdf', open('cds_copy.pdf', 'rb'))
print("File Collected")
ftp.quit()
f.close()
s.close()
ERROR:错误:
$python3 client.py
Receiving data...
Traceback (most recent call last):
File "client.py", line 17, in <module>
ftp.retrbinary('RETR cds_copy.pdf', f.write, 1024)
File "/usr/lib/python3.6/ftplib.py", line 441, in retrbinary
self.voidcmd('TYPE I')
File "/usr/lib/python3.6/ftplib.py", line 277, in voidcmd
self.putcmd(cmd)
File "/usr/lib/python3.6/ftplib.py", line 199, in putcmd
self.putline(line)
File "/usr/lib/python3.6/ftplib.py", line 194, in putline
self.sock.sendall(line.encode(self.encoding))
AttributeError: 'NoneType' object has no attribute 'sendall'
Unable to figure out the error.无法弄清楚错误。
Any suggestions will be helpful.任何建议都会有所帮助。 Thanking You.
感谢您。
FTP is an application protocol as defined in RFC 959 . FTP 是RFC 959 中定义的应用程序协议。 If you want to use it in your client you must have a server speaking that FTP protocol.
如果你想在你的客户端中使用它,你必须有一个使用该 FTP 协议的服务器。 Your server does not speak the FTP protocol but simply dumps the content of a file to the client.
您的服务器不使用 FTP 协议,而只是将文件内容转储到客户端。 In this case the client should expect exactly this and not speak FTP, ie
在这种情况下,客户端应该完全期望这一点,而不是说 FTP,即
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.43.16", 9999))
f = open("out.pdf","wb")
while True:
buf = s.recv(1024)
if buf == "":
break
f.write(buf)
Apart from that your server is broken: it first reads 1024 bytes from the file, discards it and then reads the next 1024 byte and sends it to the client:除此之外,您的服务器已损坏:它首先从文件中读取 1024 个字节,将其丢弃,然后读取下一个 1024 个字节并将其发送到客户端:
while f.read(1024): # read 1024 byte from f but discard
socket_object.send(f.read(1024)) # read another 1024 byte from f and send
This is likely not what you've intended.这可能不是您想要的。 Instead it should look more like this:
相反,它应该看起来更像这样:
while True:
buf = f.read(1024) # read bytes from f
if buf == "": # check that not done
break
socket_object.sendall(buf) # write previously read bytes to client
Note that this also uses sendall
instead of send
since only sendall
will take care to actually send all given data.请注意,这也使用
sendall
而不是send
因为只有sendall
会注意实际发送所有给定的数据。 A simple send
instead might only send part of the data and you have to check the return value to find out how much was send.相反,简单的
send
可能只发送部分数据,您必须检查返回值以了解发送了多少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.