Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用Paramiko从另一台服务器执行远程python代码以发送带有附件的电子邮件_Python_Ssh_Email Attachments_Paramiko_Smtplib - Fatal编程技术网

无法使用Paramiko从另一台服务器执行远程python代码以发送带有附件的电子邮件

无法使用Paramiko从另一台服务器执行远程python代码以发送带有附件的电子邮件,python,ssh,email-attachments,paramiko,smtplib,Python,Ssh,Email Attachments,Paramiko,Smtplib,我在server1上编写了一个python脚本来执行另一个python脚本,该脚本名为sendmail.py,位于服务器2上 相同的代码如下所示: import paramiko import smtplib import base64 ftpServer = "servname ftpUser = "username" ftpPwd = "*****" port = 22 try: client = paramiko.SSHClient() client.load

我在server1上编写了一个python脚本来执行另一个python脚本,该脚本名为sendmail.py,位于服务器2上

相同的代码如下所示:

import paramiko
import smtplib
import base64

ftpServer = "servname
ftpUser   = "username"
ftpPwd    = "*****"
port = 22

try:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(ftpServer, port, ftpUser, ftpPwd)
    print "Connection established with " + ftpServer

    stdin, stdout, stderr = client.exec_command('python Codes/sendmail.py')
    client.close()
except Exception, ex:
    print "Error in sending e-mail: ", ex
另一方面,server2 sendmail.py上的python代码如下所示,我正试图在上述脚本中执行该代码:

# Sending mail with an attachment file 'abc.txt'
# sendmail.py

import smtplib
import base64

filename = "abc.txt"

# Read a file and encode it into base64 format
fo = open(filename, "rb")
fo = open(filename, "rb")
filecontent = fo.read()
encodedcontent = base64.b64encode(filecontent)  # base64

sender   = 'xyz@gmail.com'
receiver = 'xyz@gmail.com'

marker = "AUNIQUEMARKER"

body ="""
This is a test email sent with an attachement.
"""
# Define the main headers.
part1 = """From: xyz@gmail.com
Subject: Trial mail
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)

# Define the message action
part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit

%s
--%s
""" % (body,marker)

# Define the attachment section
part3 = """Content-Type: multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=%s

%s
--%s--
""" %(filename, filename, encodedcontent, marker)
message = part1 + part2 + part3

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receiver, message)
   print "Successfully sent email"
except Exception, ex:
   print "Error: unable to send email", ex
单独而言,server2上的sendmail.py代码运行良好。但问题是我无法通过server1上的paramiko从python代码执行相同的代码。没有抛出错误,只是程序正常退出,没有发送邮件

尝试通过在计算机中的不同位置打印来调试相同的 代码sendmail.py,我发现在server1上运行的代码在 进入sendmail.py的这一行:

我不想将文件读取到当前服务器,但它应该从server2发送邮件,因为这是独立发生的。 请帮忙

谢谢

fo = open(filename, "rb")