Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def _get_file_type(self, ext):
- ''' get mime-filetype from ext '''
- types = {
- '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', }
- if ext in types:
- return types[ext]
- else:
- return 'application/octet-stream'
- # сюда передаем массив вот так: data = {'test': '123456', 'file': '@my/file/path.txt'}
- # все значения которые начинаются с @ - он обрабатывает как приаттаченные файлы
- def postfile(self, url, data):
- self.post_data = b''
- form_fields = []
- files = []
- boundary = '--------' + str(int(time.time()))
- for name, value in data.items():
- # if file
- value = str(value)
- if value.startswith('@'):
- value = value[1:]
- with open(value, 'rb') as imageHnd:
- body = imageHnd.read()
- ext = hlp.parse('\.([a-z0-9]+)', value.lower())
- if ext is False:
- type = 'text/plain'
- else:
- type = self._get_file_type(ext)
- #mimetype = mimetypes.guess_type(value, strict=False)[0] or 'application/octet-stream'
- files.append((name, value, type, body))
- # if simple key=val
- else:
- form_fields.append((name, value))
- #create post-data
- parts = []
- part_boundary = '--' + boundary
- # Add the form fields
- parts.extend(
- [ part_boundary,
- 'Content-Disposition: form-data; name="%s"' % name,
- '',
- value,
- ]
- for name, value in form_fields
- )
- # Add the files to upload
- parts2 = [
- [ bytes(part_boundary,'utf8'),
- bytes('Content-Disposition: form-data; name="%s"; filename="%s"' % \
- (field_name, os.path.basename(filename)),'utf8'),
- bytes('Content-Type: %s' % content_type, 'utf8'),
- b'',
- body,
- ]
- for field_name, filename, content_type, body in files
- ]
- # Flatten the list and add closing boundary marker,
- # then return CR+LF separated data
- flattened = list(itertools.chain(*parts))
- flattened = [bytes(row, 'utf8') for row in flattened]
- flattened.extend(list(itertools.chain(*parts2)))
- flattened.append(b'--' + bytes(boundary, 'utf8') + b'--')
- flattened.append(b'')
- self.post_data = b'\r\n'.join(flattened)
- self.content_type = 'multipart/form-data; boundary=%s' % boundary
- self.url = url
- self._exec(formdata=True)
- return self.response
- =================
- def _exec(self, decode=True, formdata=False, saveDebug=True, fname='', failSafeUrls=None):
- ''' make request '''
- self.headers['User-Agent'] = self._get_ua()
- # cleanup
- self.response = ''
- self.last_error = ''
- request = urllib.request.Request(url)
- hnds = [urllib.request.HTTPCookieProcessor(self.cookie_jar)]
- for header, content in self.headers.items():
- request.add_header(header, content)
- method = 'get'
- if formdata:
- method = 'multipart/form-data'
- request.add_header('Content-type', self.content_type)
- request.add_header('Content-length', len(self.post_data))
- request.add_data(self.post_data)
- else:
- method = 'application/x-www-form-urlencoded'
- if self.post_data:
- request.add_data(bytes(self.post_data, 'utf8'))
- hnds.append(myRedirs())
- opener = urllib.request.build_opener(*hnds)
- urllib.request.install_opener(opener)
- try:
- response = urllib.request.urlopen(request,
- timeout=self.parent.cnf.getint('http_timeout'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement