code_junkie

Send file using POST from a Python script

Nov 10th, 2011
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. user_agent = "image uploader"
  2. default_message = "Image $current of $total"
  3.  
  4. import logging
  5. import os
  6. from os.path import abspath, isabs, isdir, isfile, join
  7. import random
  8. import string
  9. import sys
  10. import mimetypes
  11. import urllib2
  12. import httplib
  13. import time
  14. import re
  15.  
  16. def random_string (length):
  17. return ''.join (random.choice (string.letters) for ii in range (length + 1))
  18.  
  19. def encode_multipart_data (data, files):
  20. boundary = random_string (30)
  21.  
  22. def get_content_type (filename):
  23. return mimetypes.guess_type (filename)[0] or 'application/octet-stream'
  24.  
  25. def encode_field (field_name):
  26. return ('--' + boundary,
  27. 'Content-Disposition: form-data; name="%s"' % field_name,
  28. '', str (data [field_name]))
  29.  
  30. def encode_file (field_name):
  31. filename = files [field_name]
  32. return ('--' + boundary,
  33. 'Content-Disposition: form-data; name="%s"; filename="%s"' % (field_name, filename),
  34. 'Content-Type: %s' % get_content_type(filename),
  35. '', open (filename, 'rb').read ())
  36.  
  37. lines = []
  38. for name in data:
  39. lines.extend (encode_field (name))
  40. for name in files:
  41. lines.extend (encode_file (name))
  42. lines.extend (('--%s--' % boundary, ''))
  43. body = 'rn'.join (lines)
  44.  
  45. headers = {'content-type': 'multipart/form-data; boundary=' + boundary,
  46. 'content-length': str (len (body))}
  47.  
  48. return body, headers
  49.  
  50. def send_post (url, data, files):
  51. req = urllib2.Request (url)
  52. connection = httplib.HTTPConnection (req.get_host ())
  53. connection.request ('POST', req.get_selector (),
  54. *encode_multipart_data (data, files))
  55. response = connection.getresponse ()
  56. logging.debug ('response = %s', response.read ())
  57. logging.debug ('Code: %s %s', response.status, response.reason)
  58.  
  59. def make_upload_file (server, thread, delay = 15, message = None,
  60. username = None, email = None, password = None):
  61.  
  62. delay = max (int (delay or '0'), 15)
  63.  
  64. def upload_file (path, current, total):
  65. assert isabs (path)
  66. assert isfile (path)
  67.  
  68. logging.debug ('Uploading %r to %r', path, server)
  69. message_template = string.Template (message or default_message)
  70.  
  71. data = {'MAX_FILE_SIZE': '3145728',
  72. 'sub': '',
  73. 'mode': 'regist',
  74. 'com': message_template.safe_substitute (current = current, total = total),
  75. 'resto': thread,
  76. 'name': username or '',
  77. 'email': email or '',
  78. 'pwd': password or random_string (20),}
  79. files = {'upfile': path}
  80.  
  81. send_post (server, data, files)
  82.  
  83. logging.info ('Uploaded %r', path)
  84. rand_delay = random.randint (delay, delay + 5)
  85. logging.debug ('Sleeping for %.2f seconds------------------------------nn', rand_delay)
  86. time.sleep (rand_delay)
  87.  
  88. return upload_file
  89.  
  90. def upload_directory (path, upload_file):
  91. assert isabs (path)
  92. assert isdir (path)
  93.  
  94. matching_filenames = []
  95. file_matcher = re.compile (r'.(?:jpe?g|gif|png)$', re.IGNORECASE)
  96.  
  97. for dirpath, dirnames, filenames in os.walk (path):
  98. for name in filenames:
  99. file_path = join (dirpath, name)
  100. logging.debug ('Testing file_path %r', file_path)
  101. if file_matcher.search (file_path):
  102. matching_filenames.append (file_path)
  103. else:
  104. logging.info ('Ignoring non-image file %r', path)
  105.  
  106. total_count = len (matching_filenames)
  107. for index, file_path in enumerate (matching_filenames):
  108. upload_file (file_path, index + 1, total_count)
  109.  
  110. def run_upload (options, paths):
  111. upload_file = make_upload_file (**options)
  112.  
  113. for arg in paths:
  114. path = abspath (arg)
  115. if isdir (path):
  116. upload_directory (path, upload_file)
  117. elif isfile (path):
  118. upload_file (path)
  119. else:
  120. logging.error ('No such path: %r' % path)
  121.  
  122. logging.info ('Done!')
  123.  
  124. import urllib2
  125. class EnhancedFile(file):
  126. def __init__(self, *args, **keyws):
  127. file.__init__(self, *args, **keyws)
  128.  
  129. def __len__(self):
  130. return int(os.fstat(self.fileno())[6])
  131.  
  132. theFile = EnhancedFile('a.xml', 'r')
  133. theUrl = "http://example.com/abcde"
  134. theHeaders= {'Content-Type': 'text/xml'}
  135.  
  136. theRequest = urllib2.Request(theUrl, theFile, theHeaders)
  137.  
  138. response = urllib2.urlopen(theRequest)
  139.  
  140. theFile.close()
  141.  
  142.  
  143. for line in response:
  144. print line
  145.  
  146. import urllib2
  147.  
  148. if __name__ == "__main__":
  149. conn = urllib2.urlopen(u"http://localhost/someurl.cgi",
  150. u"This is your POST data")
  151.  
  152. # Do something with the response.
Add Comment
Please, Sign In to add comment