Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.45 KB | None | 0 0
  1. def _get_file_type(self, ext):
  2.         ''' get mime-filetype from ext '''
  3.  
  4.         types = {
  5. 'js': 'text/javascript', 'gif': 'image/gif', 'jpeg': 'image/jpg', 'jpe': 'image/jpg', 'jpg': 'image/jpg', 'bmp': 'image/x-ms-bmp', 'css': 'text/css', 'htm': 'text/html', 'html': 'text/html', 'shtml': 'text/html', 'txt': 'text/plain', 'xml': 'text/xml', 'xht': 'application/xhtml+xml', 'xhtm': 'application/xhtml+xml', 'xhtml': 'application/xhtml+xml', 'rss': 'application/rss+xml', 'atom': 'application/atom+xml', 'xslt': 'application/xslt+xml', 'rdf': 'application/rdf+xml', 'wml': 'application/wml+xml', 'svg': 'image/svg+xml', 'svgz': 'image/svg+xml', 'ico': 'image/x-icon', 'png': 'image/png', 'wav': 'audio/wav', 'wav': 'audio/x-wav', 'avi': 'video/x-msvideo', 'mpeg': 'video/mpeg', 'mpg': 'video/mpeg', 'mpe': 'video/mpeg', 'm2v': 'video/mpeg', 'm1v': 'video/mpeg', 'mpa': 'video/mpeg', 'mp4': 'video/mp4', 'mpg4': 'video/mp4', 'ogg': 'application/ogg', 'mp3': 'audio/mp3', 'ttf': 'application/x-font-ttf', 'ttc': 'application/x-font-ttf', 'z': 'application/x-compress', 'gz': 'application/x-gzip', 'gzip': 'application/x-gzip', 'tgz': 'application/x-gzip', 'bz2': 'application/bzip2', 'tbz': 'application/bzip2', 'tbz2': 'application/bzip2', 'lzma': 'application/x-lzma', 'tlz': 'application/x-lzma', 'tlzma': 'application/x-lzma', 'xz': 'application/x-xz', 'txz': 'application/x-xz', 'tar': 'application/x-tar', 'tgz': 'application/x-tar', 'gz': 'application/x-tar', 'tbz': 'application/x-tar', 'tbz2': 'application/x-tar', 'bz2': 'application/x-tar', 'tlz': 'application/x-tar', 'tlzma': 'application/x-tar', 'lzma': 'application/x-tar', 'txz': 'application/x-tar', 'xz': 'application/x-tar', 'rpm': 'application/x-rpm', 'pdf': 'application/pdf', 'tif': 'image/tiff', 'tiff': 'image/tiff', 'exe': 'application/x-msdownload', 'dll': 'application/x-msdownload', 'bat': 'application/x-msdownload', 'pif': 'application/x-msdownload', 'com': 'application/x-msdownload', 'scr': 'application/x-msdownload', 'msi': 'application/x-msdownload', 'url': 'application/internet-shortcut', 'zip': 'application/x-zip-compressed', 'rar': 'application/x-rar-compressed', 'doc': 'application/msword', 'dot': 'application/msword', 'wiz': 'application/msword', 'wzs': 'application/msword', 'docx': 'application/msword', 'rtf': 'application/rtf', 'rtx': 'text/richtext', 'xls': 'application/vnd.ms-excel', 'xl': 'application/vnd.ms-excel', 'xla': 'application/vnd.ms-excel', 'xlb': 'application/vnd.ms-excel', 'xlc': 'application/vnd.ms-excel', 'xld': 'application/vnd.ms-excel', 'xlk': 'application/vnd.ms-excel', 'xll': 'application/vnd.ms-excel', 'xlm': 'application/vnd.ms-excel', 'xlt': 'application/vnd.ms-excel', 'xlv': 'application/vnd.ms-excel', 'xlw': 'application/vnd.ms-excel', 'csv': 'application/vnd.ms-excel', 'xlsx': 'application/vnd.ms-excel', 'xltx': 'application/vnd.ms-excel', 'pot': 'application/vnd.ms-powerpoint', 'ppa': 'application/vnd.ms-powerpoint', 'pps': 'application/vnd.ms-powerpoint', 'ppt': 'application/vnd.ms-powerpoint', 'pwz': 'application/vnd.ms-powerpoint', 'pptx': 'application/vnd.ms-powerpoint', 'ppsx': 'application/vnd.ms-powerpoint', 'potx': 'application/vnd.ms-powerpoint', 'sldx': 'application/vnd.ms-powerpoint', 'wma': 'audio/x-ms-wma', 'wmv': 'video/x-ms-wmv', 'wmx': 'video/x-ms-wmv', '3gp': 'video/3gpp', 'flac': 'audio/flac', }
  6.  
  7.         if ext in types:
  8.             return types[ext]
  9.         else:
  10.             return 'application/octet-stream'
  11.  
  12.  
  13.  
  14.  
  15. # сюда передаем массив вот так:  data = {'test': '123456', 'file': '@my/file/path.txt'}
  16. # все значения которые начинаются с @ - он обрабатывает как приаттаченные файлы
  17.  
  18.     def postfile(self, url, data):
  19.  
  20.         self.post_data = b''
  21.         form_fields = []
  22.         files = []
  23.         boundary = '--------' + str(int(time.time()))
  24.  
  25.         for name, value in data.items():
  26.             # if file
  27.             value = str(value)
  28.             if value.startswith('@'):
  29.  
  30.                 value = value[1:]
  31.                 with open(value, 'rb') as imageHnd:
  32.                     body = imageHnd.read()
  33.  
  34.                 ext = hlp.parse('\.([a-z0-9]+)', value.lower())
  35.                 if ext is False:
  36.                     type = 'text/plain'
  37.                 else:
  38.                     type = self._get_file_type(ext)
  39.  
  40.                 #mimetype = mimetypes.guess_type(value, strict=False)[0] or 'application/octet-stream'
  41.                 files.append((name, value, type, body))
  42.             # if simple key=val
  43.             else:
  44.                 form_fields.append((name, value))
  45.  
  46.         #create post-data
  47.         parts = []
  48.         part_boundary = '--' + boundary
  49.  
  50.         # Add the form fields
  51.         parts.extend(
  52.             [ part_boundary,
  53.               'Content-Disposition: form-data; name="%s"' % name,
  54.               '',
  55.               value,
  56.             ]
  57.             for name, value in form_fields
  58.             )
  59.  
  60.         # Add the files to upload
  61.         parts2 = [
  62.             [ bytes(part_boundary,'utf8'),
  63.               bytes('Content-Disposition: form-data; name="%s"; filename="%s"' % \
  64.                  (field_name, os.path.basename(filename)),'utf8'),
  65.               bytes('Content-Type: %s' % content_type, 'utf8'),
  66.               b'',
  67.               body,
  68.             ]
  69.             for field_name, filename, content_type, body in files
  70.             ]
  71.  
  72.         # Flatten the list and add closing boundary marker,
  73.         # then return CR+LF separated data
  74.         flattened = list(itertools.chain(*parts))
  75.         flattened = [bytes(row, 'utf8') for row in flattened]
  76.         flattened.extend(list(itertools.chain(*parts2)))
  77.         flattened.append(b'--' + bytes(boundary, 'utf8') + b'--')
  78.         flattened.append(b'')
  79.         self.post_data = b'\r\n'.join(flattened)
  80.  
  81.         self.content_type = 'multipart/form-data; boundary=%s' % boundary
  82.  
  83.         self.url = url
  84.         self._exec(formdata=True)
  85.         return self.response
  86.  
  87.  
  88. =================
  89.  
  90.     def _exec(self, decode=True, formdata=False, saveDebug=True, fname='', failSafeUrls=None):
  91.         ''' make request '''
  92.  
  93.         self.headers['User-Agent'] = self._get_ua()
  94.  
  95.         # cleanup
  96.         self.response = ''
  97.         self.last_error = ''
  98.  
  99.         request = urllib.request.Request(url)
  100.  
  101.         hnds = [urllib.request.HTTPCookieProcessor(self.cookie_jar)]
  102.  
  103.         for header, content in self.headers.items():
  104.             request.add_header(header, content)
  105.  
  106.         method = 'get'
  107.         if formdata:
  108.             method = 'multipart/form-data'
  109.             request.add_header('Content-type', self.content_type)
  110.             request.add_header('Content-length', len(self.post_data))
  111.             request.add_data(self.post_data)
  112.         else:
  113.             method = 'application/x-www-form-urlencoded'
  114.             if self.post_data:
  115.                 request.add_data(bytes(self.post_data, 'utf8'))
  116.  
  117.  
  118.         hnds.append(myRedirs())
  119.         opener = urllib.request.build_opener(*hnds)
  120.         urllib.request.install_opener(opener)
  121.  
  122.         try:
  123.             response = urllib.request.urlopen(request,
  124.                     timeout=self.parent.cnf.getint('http_timeout'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement