Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. cnopts = pysftp.CnOpts()
  2. cnopts.hostkeys = None
  3.  
  4. import pysftp
  5. import sys
  6. import os
  7. import smtplib
  8. from datetime import datetime
  9. from email.mime.text import MIMEText
  10.  
  11.  
  12. def main():
  13. dir = []
  14. files = []
  15. try:
  16. srv = pysftp.Connection(host="xxxx", username="xxxx", password="xxxx" )
  17. except Exception as e:
  18. send_email(u'Connection error: ' + e.message)
  19.  
  20. with srv.cd():
  21. srv.chdir("file location")
  22. files = srv.listdir()
  23. dt = datetime.now().strftime('%Y%m%d')
  24. files = [f for f in files if 'SOPO_147_Consolidated_Plant_BATCHECP_{}_0'.format(dt) in f]
  25. if len(files) != 1:
  26. send_email('Error: No file found on FTP path. Please check whether file is posted for today')
  27. return
  28.  
  29. _filename = "filelocation" + files[0]
  30. srv.get(_filename)
  31.  
  32. # Closes the connection
  33. srv.close()
  34.  
  35. # rename the file
  36. new_filename = 'location'
  37. # new_filename = 'Location'
  38. try:
  39. os.remove(new_filename)
  40. except Exception as e:
  41. send_email(u'Move error: ' + e.message)
  42.  
  43. os.rename(files[0], new_filename)
  44.  
  45.  
  46. def send_email(message):
  47. ''' Send email to support alias'''
  48. msg = MIMEText(message)
  49.  
  50. sender = 'shaan.tableau@gmail.com'
  51. support_alias = 'shaan.tableau@gmail.com'
  52. msg['Subject'] = 'Test file '
  53. msg['From'] = sender
  54. msg['To'] = support_alias
  55.  
  56. # Send the message via our own SMTP server, but don't include the
  57. # envelope header.
  58. s = smtplib.SMTP('smtp.net')
  59. s.sendmail(sender, [support_alias], msg.as_string())
  60. s.quit()
  61.  
  62.  
  63. if __name__ == '__main__':
  64. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement