Advertisement
Guest User

Untitled

a guest
May 1st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. # encoding: utf-8
  2.  
  3. """ Python General Helpers
  4. Copyright (c) 2010 Kenneth Reitz. Creative Commons Attribution 3.0 Lisense.
  5. """
  6.  
  7. import urllib, re, time
  8. import paramiko
  9.  
  10. def enc(str):
  11. """Encodes a string to ascii"""
  12. return str.encode('ascii', 'ignore')
  13.  
  14. def dec(str):
  15. """Decodes a string to ascii"""
  16. return str.decode('ascii', 'ignore')
  17.  
  18. def get_file(path):
  19. """Returns a file as a string"""
  20. return open(path, 'r').read()
  21.  
  22. def get_file_lines(path):
  23. """Returns a file as a list of strings"""
  24. return open(path, 'r').readlines()
  25.  
  26. def get_http(uri):
  27. """Fetches a file over http as a string"""
  28. return urllib.urlopen(uri).read()
  29.  
  30. def get_http_lines(uri):
  31. """Fetches a file over http as a file object"""
  32. return urllib.urlopen(uri).readlines()
  33.  
  34. def get_sftp(hostname, username, password, filename):
  35. """Fetches a file over sftp as a string
  36. Note: Server must be in known_hosts.
  37. """
  38.  
  39. client = paramiko.SSHClient()
  40. client.load_host_keys(keysPath=None)
  41. client.connect(hostname=hostname, username=username, password=password)
  42.  
  43. file = client.open_sftp().open(filename).read()
  44. log("Download of %s complete" % filename)
  45. return file
  46.  
  47. def filename_from_url(url):
  48. """Returns filename from url"""
  49. search = "(\w|[-.])+$"
  50. return re.search(search, url).group()
  51.  
  52. def sql_datetime():
  53. """Returns SQL DateTime NOW() string"""
  54. return str(time.strftime("%Y-%m-%d %H:%M:%S"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement