[英]Read PDF and send via FTP in Python
Usecase : I am trying to read pdf from url and then send it via FTP 用例:我正在尝试从url阅读pdf,然后通过FTP发送
I have functions as: 我的职能是:
def send_via_ftp(self, url, filename, ftp_site, username, password, directory):
import urllib2
try:
data = urllib2.urlopen(url)
except urllib2.URLError, e:
print "Failed to fetch content: %s" % e
return False
except urllib2.HTTPError, e:
print "HTTP ERROR: %s" % e
return False
return self.send_file_by_ftp(data, ftp_site, username, password, directory, filename)
def send_file_by_ftp(self, data, ftp_site, username, password, directory, filename):
import ftplib
try:
remote_ftp_connection = ftplib.FTP(ftp_site)
except ftplib.all_errors as e:
print str(e)
return False
else:
remote_ftp_connection.login(username, password)
try:
if len(directory):
remote_ftp_connection.cwd(directory)
remote_ftp_connection.storbinary("STOR %s" % filename, data)
except ftplib.error_perm,e:
print str(e)
return False
else:
remote_ftp_connection.quit()
return True
My call looks like: send_via_ftp(" http://url/ ***.pdf", "XYZ.pdf", "ftp url 192.168.0.101", "XXXX", "YYYYY", "") 我的呼叫看起来像:send_via_ftp(“ http:// url / ***。pdf”,“ XYZ.pdf”,“ ftp url 192.168.0.101”,“ XXXX”,“ YYYYY”,“”)
The file is successfully made in the FTP folder but the content in the file are not written. 已成功在FTP文件夹中创建文件,但未写入文件中的内容。 While I open it says "Format Error: Not a pdf or corrupted".
当我打开它时,显示“格式错误:不是pdf或已损坏”。 What could be the problem?
可能是什么问题呢? Many thanks for any help
非常感谢您的帮助
Looks like you're passing data
as the second parameter to remote_ftp_connection.storbinary
, but it should be ftp_data
. 看起来您要将
data
作为第二个参数传递给remote_ftp_connection.storbinary
,但它应该是ftp_data
。 Not sure why you aren't getting a NameError
exception. 不知道为什么您没有收到
NameError
异常。
Since FTP uses file-like object, the following modification worked for me. 由于FTP使用类似文件的对象,因此以下修改对我有用。
def send_via_ftp(self, url, filename, ftp_site, username, password, directory):
import urllib2
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
try:
data = urllib2.urlopen(url)
data_ = StringIO(data.read())
data_.seek(0)
except urllib2.URLError, e:
print "Failed to fetch content: %s" % e
return False
except urllib2.HTTPError, e:
print "HTTP ERROR: %s" % e
return False
else:
content = data_.read()
return self.send_file_by_ftp(content, ftp_site, username, password, directory, filename)
================================================================================ ================================================== =============================
def send_file_by_ftp(self, data, ftp_site, username, password, directory, filename):
import ftplib
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
try:
remote_ftp_connection = ftplib.FTP(ftp_site)
except ftplib.all_errors as e:
print str(e)
return False
else:
remote_ftp_connection.login(username, password)
try:
if len(directory):
remote_ftp_connection.cwd(directory)
remote_ftp_connection.storbinary("STOR %s" % filename, StringIO(data))
except ftplib.error_perm,e:
print str(e)
return False
else:
remote_ftp_connection.quit()
return True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.