def create_attachment(ox, task): from requests.packages.urllib3.fields import RequestField from requests.packages.urllib3.filepost import encode_multipart_formdata url = ox._url('attachment', 'attach') params = ox._params() json_0 = {'module': task.module_type, 'attached': task.id, 'folder': task.folder_id} fields = [] rf = RequestField(name='json_0',data=json.dumps(json_0)) rf.make_multipart(content_disposition='form-data') fields.append(rf) rf = RequestField(name='file_0', data="TEXT", filename='attachment.txt') rf.make_multipart(content_disposition='form-data',content_type='text/plain') fields.append(rf) post_body, content_type = encode_multipart_formdata(fields) content_type = ''.join(('multipart/mixed',) + content_type.partition(';')[1:]) headers = {'Content-Type': content_type} response = requests.post(url, cookies=ox._cookies, params=params, headers=headers, data=post_body) if response and response.status_code == 200: regex='\((\{.*\})\)' match = re.search(regex, response.content) if match: return json.loads(match.group(1)) return None
def post(self, connection=None): if connection is None: connection = self.connection params = [] for key in ['database', 'transform', 'txid', \ 'temporal-collection', 'system-time']: if key in self._config: params.append("{}={}".format(key, self._config[key])) for pair in self.transparams: params.append("trans:{}={}".format(pair[0], pair[1])) uri = connection.client_uri("documents") if params: uri = uri + "?" + "&".join(params) self.logger.debug("Bulk POST {}: {}".format(self.field_count, uri)) post_body, content_type = encode_multipart_formdata(self.fields) post_ct = ''.join(('multipart/mixed',) \ + content_type.partition(';')[1:]) response = connection.post(uri, payload=post_body, content_type=post_ct) self.clear_content() return response
def check_read_file_with_chunks(self, file_size, read_size): #print "===== Testing with file_size=",file_size,"read_size=",read_size boundary="deterministic-test-boundary" a_file = LargeFileMock(file_size) parts = {'some_field': 'this is the value...', 'some_file': a_file.read(), } expected_bytes = encode_multipart_formdata(parts, boundary)[0] content_length = len(expected_bytes) # Now read from our encoder : a_file = LargeFileMock(file_size) parts = {'some_field': 'this is the value...', 'some_file': a_file, } encoder = MultipartEncoder(parts, boundary=boundary) raw_bytes_count = 0 while True: data = encoder.read(read_size) if not data: break #print "read",len(data),"bytes : ",repr(data) assert data == expected_bytes[raw_bytes_count:raw_bytes_count+len(data)] raw_bytes_count += len(data) #if raw_bytes_count != content_length: # print "Test failed with file_size=",file_size,"and read_size=",read_size assert raw_bytes_count == content_length
def _send(self, email_message): """A helper method that does the actual sending.""" if not email_message.recipients(): return False from_email = sanitize_address(email_message.from_email, email_message.encoding) recipients = [sanitize_address(addr, email_message.encoding) for addr in email_message.recipients()] try: post_data = [] post_data.append(("to", (",".join(recipients)))) post_data.append(("text", email_message.body)) post_data.append(("subject", email_message.subject)) post_data.append(("from", from_email)) # get our recipient variables if they were passed in recipient_variables = email_message.extra_headers.pop("recipient_variables", None) if recipient_variables is not None: post_data.append(("recipient-variables", recipient_variables)) for name, value in self._map_smtp_headers_to_api_parameters(email_message): post_data.append((name, value)) if hasattr(email_message, "alternatives") and email_message.alternatives: for alt in email_message.alternatives: if alt[1] == "text/html": post_data.append(("html", alt[0])) break # Map Reply-To header if present try: if email_message.reply_to: post_data.append(("h:Reply-To", ", ".join(map(force_text, email_message.reply_to)))) except AttributeError: pass if email_message.attachments: for attachment in email_message.attachments: post_data.append(("attachment", (attachment[0], attachment[1]))) content, header = encode_multipart_formdata(post_data) headers = {"Content-Type": header} else: content = post_data headers = None response = requests.post( self._api_url + "messages", auth=("api", self._access_key), data=content, headers=headers ) except: if not self.fail_silently: raise return False if response.status_code != 200: if not self.fail_silently: raise MailgunAPIError(response) return False return True
def appSDKification(folder, progress): ''' zip app folder and send to WAB compiler to apply SDK compilation. The returned zip will be the official webapp ''' progress.oscillate() progress.setText("Get Authorization token") try: global __appSDKification_doAgain if __appSDKification_doAgain: QgsMessageLog.logMessage("Renew token in case of it is expired and retry", level=QgsMessageLog.WARNING) utils.resetCachedToken() token = utils.getToken() except Exception as e: pub.sendMessage(utils.topics.endAppSDKification, success=False, reason=str(e)) return # zip folder to send for compiling progress.setText("Preparing data to compile") zipFileName = tempFilenameInTempFolder("webapp.zip", "webappbuilder") try: with zipfile.ZipFile(zipFileName, "w") as zf: relativeFrom = os.path.dirname(folder) for dirname, subdirs, files in os.walk(folder): # exclude data folder if 'data' in subdirs: subdirs.remove('data') if relativeFrom in dirname: zf.write(dirname, dirname[len(relativeFrom):]) for filename in files: fiename = os.path.join(dirname, filename) zf.write(fiename, fiename[len(relativeFrom):]) except: msg = "Could not zip webapp folder: {}".format(folder) pub.sendMessage(utils.topics.endAppSDKification, success=False, reason=msg) return # prepare data for WAB compiling request with open(zipFileName, 'rb') as f: fileContent = f.read() fields = { 'file': (os.path.basename(zipFileName), fileContent) } payload, content_type = encode_multipart_formdata(fields) headers = {} headers["authorization"] = "Bearer {}".format(token) headers["Content-Type"] = content_type # prepare request (as in NetworkAccessManager) but without blocking request # do http post progress.setText("Wait compilation") global __anam if __anam: del __anam __anam = None __anam = NetworkAccessManager(debug=pluginSetting("logresponse")) __anam.request(utils.wabCompilerUrl(), method='POST', body=payload, headers=headers, blocking=False) __anam.reply.finished.connect( lambda: manageFinished(__anam, zipFileName, folder, progress) )
def _add_multipart(parts): mime_multipart_parts = list() for name, (filename, data, content_type) in parts.items(): multipart_part = RequestField(name=name, data=data, filename=filename) multipart_part.make_multipart(content_type=content_type) mime_multipart_parts.append(multipart_part) xml_request, content_type = encode_multipart_formdata(mime_multipart_parts) content_type = ''.join(('multipart/mixed',) + content_type.partition(';')[1:]) return xml_request, content_type
def _send(self, email_message): """A helper method that does the actual sending.""" if not email_message.recipients(): return False from_email = sanitize_address(email_message.from_email, email_message.encoding) recipients = [sanitize_address(addr, email_message.encoding) for addr in email_message.recipients()] try: from requests.packages.urllib3.filepost import encode_multipart_formdata post_data = [] post_data.append(('to', (",".join(recipients)),)) post_data.append(('text', email_message.body,)) post_data.append(('subject', email_message.subject,)) post_data.append(('from', from_email,)) if 'Reply-To' in email_message.extra_headers: reply_to = email_message.extra_headers['Reply-To'] reply_to = sanitize_address(reply_to, email_message.encoding) post_data.append(('h:Reply-To', reply_to,)) if 'X-Mailgun-Variables' in email_message.extra_headers: custom_data = email_message.extra_headers['X-Mailgun-Variables'] post_data.append(('v:my-custom-data', json.dumps(custom_data))) if hasattr(email_message, 'alternatives') and email_message.alternatives: for alt in email_message.alternatives: if alt[1] == 'text/html': post_data.append(('html', alt[0],)) break if email_message.attachments: for attachment in email_message.attachments: post_data.append(('attachment', (attachment[0], attachment[1],))) content, header = encode_multipart_formdata(post_data) headers = {'Content-Type': header} else: content = post_data headers = None response = requests.post(self._api_url + "messages", auth=("api", self._access_key), data=content, headers=headers) except: if not self.fail_silently: raise return False if response.status_code != 200: if not self.fail_silently: raise MailgunAPIError(response) return False return True
def upload(name, srcFp, size, copied, progress, forceOverwrite=True): """ Reference link: http://www.kuaipan.cn/developers/document_apiupload.htm """ url = "{}1/fileops/upload_file".format(uploadLocate()["url"]) parameters = {"path": name, "root": "app_folder", "overwrite": forceOverwrite} (data, ctype) = encode_multipart_formdata({"file": (name, srcFp.read(size))}) body = BufferReader(data, progress, copied) processRequest(url, "POST", parameters, data=body, headers={"Content-Type": ctype}) return body.done, body.stop
def uplaod(accessToken, filePath, mediaType): openFile = open(filePath, "rb") param = {'media': openFile.read()} postData, content_type = encode_multipart_formdata(param) postUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s" % ( accessToken, mediaType) headers = {'Content-Type': content_type} files = {'media': open(filePath, "rb")} urlResp = requests.post(postUrl, files=files) #print(urlResp.text) return json.loads(urlResp.text)['media_id']
def _send(self, email_message): """A helper method that does the actual sending.""" if not email_message.recipients(): return False from_email = sanitize_address(email_message.from_email, email_message.encoding) recipients = [sanitize_address(addr, email_message.encoding) for addr in email_message.recipients()] try: post_data = [] post_data.append(('to', (",".join(recipients)),)) post_data.append(('text', email_message.body,)) post_data.append(('subject', email_message.subject,)) post_data.append(('from', from_email,)) # get our recipient variables if they were passed in recipient_variables = email_message.extra_headers.pop('recipient_variables', None) if recipient_variables is not None: post_data.append(('recipient-variables', recipient_variables, )) for name, value in self._map_smtp_headers_to_api_parameters(email_message): post_data.append((name, value, )) if hasattr(email_message, 'alternatives') and email_message.alternatives: for alt in email_message.alternatives: if alt[1] == 'text/html': post_data.append(('html', alt[0],)) break if email_message.attachments: for attachment in email_message.attachments: post_data.append(('attachment', (attachment[0], attachment[1],))) content, header = encode_multipart_formdata(post_data) headers = {'Content-Type': header} else: content = post_data headers = None response = requests.post(self._api_url + "messages", auth=("api", self._access_key), data=content, headers=headers) except: if not self.fail_silently: raise return False if response.status_code != 200: if not self.fail_silently: raise MailgunAPIError(response) return False return True
def push_document(self, filename, filestream, send=None, speed=None, color=None): """ Upload a document to pingen.com and eventually ask to send it :param str filename: name of the file to push :param StringIO filestream: file to push :param boolean send: if True, the document will be sent by pingen.com :param int/str speed: sending speed of the document if it is send 1 = Priority, 2 = Economy :param int/str color: type of print, 0 = B/W, 1 = Color :return: tuple with 3 items: 1. document_id on pingen.com 2. post_id on pingen.com if it has been sent or None 3. dict of the created item on pingen (details) """ data = { 'send': send, 'speed': speed, 'color': color, } # we cannot use the `files` param alongside # with the `datas`param when data is a # JSON-encoded data. We have to construct # the entire body and send it to `data` # https://github.com/kennethreitz/requests/issues/950 formdata = { 'file': (filename, filestream.read()), 'data': json.dumps(data), } multipart, content_type = encode_multipart_formdata(formdata) response = self._send(self.session.post, 'document/upload', headers={'Content-Type': content_type}, data=multipart) rjson = response.json document_id = rjson['id'] if rjson.get('send'): # confusing name but send_id is the posted id posted_id = rjson['send'][0]['send_id'] item = rjson['item'] return document_id, posted_id, item
def _put_mixed(self, data, target, connection): """ Put the document using the bulk interface (because it has arbitrary metadata). """ params = [] for key in ['database', 'transform', 'txid', \ 'temporal-collection', 'system-time']: if key in self._config: params.append("{}={}".format(key, self._config[key])) for pair in self.transparams: params.append("trans:{}={}".format(pair[0], pair[1])) meta = self.metadata() if self.metadata_content_type() is None: metact = "application/xml" else: metact = self.metadata_content_type() uri = connection.client_uri("documents") if params: uri = uri + "?" + "&".join(params) datact = self._config['content-type'] fields = [] rf = RequestField(name="meta1", data=meta, filename=target) rf.make_multipart(content_disposition='attachment; category=metadata', \ content_type=metact) fields.append(rf) rf = RequestField(name="data1", data=data, filename=target) rf.make_multipart(content_disposition='attachment', \ content_type=datact) fields.append(rf) post_body, content_type = encode_multipart_formdata(fields) post_ct = ''.join(('multipart/mixed',) \ + content_type.partition(';')[1:]) response = connection.post(uri, payload=post_body, content_type=post_ct) return response
def push_document(self, filename, filestream, send=None, speed=None, color=None): """ Upload a document to pingen.com and eventually ask to send it :param str filename: name of the file to push :param StringIO filestream: file to push :param boolean send: if True, the document will be sent by pingen.com :param int/str speed: sending speed of the document if it is send 1 = Priority, 2 = Economy :param int/str color: type of print, 0 = B/W, 1 = Color :return: tuple with 3 items: 1. document_id on pingen.com 2. post_id on pingen.com if it has been sent or None 3. dict of the created item on pingen (details) """ data = { 'send': send, 'speed': speed, 'color': color, } # we cannot use the `files` param alongside # with the `datas`param when data is a # JSON-encoded data. We have to construct # the entire body and send it to `data` # https://github.com/kennethreitz/requests/issues/950 formdata = { 'file': (filename, filestream.read()), 'data': json.dumps(data), } multipart, content_type = encode_multipart_formdata(formdata) response = self._send( self.session.post, 'document/upload', headers={'Content-Type': content_type}, data=multipart) rjson = response.json document_id = rjson['id'] if rjson.get('send'): # confusing name but send_id is the posted id posted_id = rjson['send'][0]['send_id'] item = rjson['item'] return document_id, posted_id, item
def _make_multipart(parts): """ Creates one "chunk" for a multi-part upload 'parts' is a dictionary that provides key-value pairs of the format name: (filename, body, content_type). Returns the post body and the content type string. For more information, see this post: http://stackoverflow.com/questions/26299889/how-to-post-multipart-list-of-json-xml-files-using-python-requests """ mime_multipart_parts = [] for name, (filename, blob, content_type) in parts.items(): multipart_part = RequestField(name=name, data=blob, filename=filename) multipart_part.make_multipart(content_type=content_type) mime_multipart_parts.append(multipart_part) post_body, content_type = encode_multipart_formdata(mime_multipart_parts) content_type = ''.join(('multipart/mixed',) + content_type.partition(';')[1:]) return post_body, content_type
def multipart_formdata(data): """Takes a dictionary (or dictionary-like object), or list of two-tuples and return a string of multipart/form-data assuming no files. :param data: dictionary-like object or list of two-tuples, e.g., ``{'key1': 'value1', 'key2': 'value2'}`` or ``[('key1', 'value1'), ('key2', 'value2')]`` :returns: str """ if data is None: return None dict(data) if isinstance(data, dict): data = data.items() return encode_multipart_formdata(data)[0].decode()
def _make_multipart(parts): """ Creates one "chunk" for a multi-part upload 'parts' is a dictionary that provides key-value pairs of the format name: (filename, body, content_type). Returns the post body and the content type string. """ mime_multipart_parts = [] for name, (filename, blob, content_type) in parts.items(): multipart_part = RequestField(name=name, data=blob, filename=filename) multipart_part.make_multipart(content_type=content_type) mime_multipart_parts.append(multipart_part) post_body, content_type = encode_multipart_formdata(mime_multipart_parts) content_type = ''.join(('multipart/mixed', ) + content_type.partition(';')[1:]) return post_body, content_type
def _put_mixed(self, data, target, connection): """ Put the document using the bulk interface (because it has arbitrary metadata). """ params = [] for key in ["database", "transform", "txid", "temporal-collection", "system-time"]: if key in self._config: params.append("{}={}".format(key, self._config[key])) for pair in self.transparams: params.append("trans:{}={}".format(pair[0], pair[1])) meta = self.metadata() if self.metadata_content_type() is None: metact = "application/xml" else: metact = self.metadata_content_type() uri = connection.client_uri("documents") if params: uri = uri + "?" + "&".join(params) datact = self._config["content-type"] fields = [] rf = RequestField(name="meta1", data=meta, filename=target) rf.make_multipart(content_disposition="attachment; category=metadata", content_type=metact) fields.append(rf) rf = RequestField(name="data1", data=data, filename=target) rf.make_multipart(content_disposition="attachment", content_type=datact) fields.append(rf) post_body, content_type = encode_multipart_formdata(fields) post_ct = "".join(("multipart/mixed",) + content_type.partition(";")[1:]) response = connection.post(uri, payload=post_body, content_type=post_ct) return response
def create_attachment(ox, task): from requests.packages.urllib3.fields import RequestField from requests.packages.urllib3.filepost import encode_multipart_formdata url = ox._url('attachment', 'attach') params = ox._params() json_0 = { 'module': task.module_type, 'attached': task.id, 'folder': task.folder_id } fields = [] rf = RequestField(name='json_0', data=json.dumps(json_0)) rf.make_multipart(content_disposition='form-data') fields.append(rf) rf = RequestField(name='file_0', data="TEXT", filename='attachment.txt') rf.make_multipart(content_disposition='form-data', content_type='text/plain') fields.append(rf) post_body, content_type = encode_multipart_formdata(fields) content_type = ''.join(('multipart/mixed', ) + content_type.partition(';')[1:]) headers = {'Content-Type': content_type} response = requests.post(url, cookies=ox._cookies, params=params, headers=headers, data=post_body) if response and response.status_code == 200: regex = '\((\{.*\})\)' match = re.search(regex, response.content) if match: return json.loads(match.group(1)) return None
def _make_multipart(parts): """ Creates one "chunk" for a multi-part upload. 'parts' is a dictionary that provides key-value pairs of the format name: (filename, body, content_type). Returns the post body and the content type string. For more information, see this post: http://stackoverflow.com/questions/26299889/how-to-post-multipart-list-of-json-xml-files-using-python-requests """ mime_multipart_parts = [] for name, (filename, blob, content_type) in parts.items(): multipart_part = RequestField(name=name, data=blob, filename=filename) multipart_part.make_multipart(content_type=content_type) mime_multipart_parts.append(multipart_part) post_body, content_type = encode_multipart_formdata(mime_multipart_parts) content_type = ''.join(('multipart/mixed',) + content_type.partition(';')[1:]) return post_body, content_type
def _compose_multipart(json_dict, filesdata): """ Composes multipart/mixed request for create/edit brain. The multipart message is constructed as 1st part application/json and subsequent file part(s). :param json: dictionary that will be json-encoded :param filesdata: dict of <filename> -> <filedata> """ # requests 1.13 does not have high-level support for multipart/mixed. # Using lower-level modules to construct multipart/mixed per # http://stackoverflow.com/questions/26299889/ # how-to-post-multipart-list-of-json-xml-files-using-python-requests fields = [] # 1st part: application/json rf = RequestField(name="project_data", data=json.dumps(json_dict)) rf.make_multipart(content_type='application/json') fields.append(rf) # Subsequent parts: file text for filename, filedata in filesdata.items(): rf = RequestField(name=filename, data=filedata, filename=filename, headers={'Content-Length': len(filedata)}) rf.make_multipart(content_disposition='attachment', content_type="application/octet-stream") fields.append(rf) # Compose message body, content_type = encode_multipart_formdata(fields) # "multipart/form-data; boundary=.." -> "multipart/mixed; boundary=.." content_type = content_type.replace("multipart/form-data", "multipart/mixed", 1) headers = {'Content-Type': content_type} return (headers, body)
def _send(self, email_message): """A helper method that does the actual sending.""" if not email_message.recipients(): return False from_email = sanitize_address(email_message.from_email, email_message.encoding) recipients = [ sanitize_address(addr, email_message.encoding) for addr in email_message.recipients() ] try: from requests.packages.urllib3.filepost import encode_multipart_formdata post_data = [] post_data.append(( 'to', (",".join(recipients)), )) post_data.append(( 'text', email_message.body, )) post_data.append(( 'subject', email_message.subject, )) post_data.append(( 'from', from_email, )) if 'Reply-To' in email_message.extra_headers: reply_to = email_message.extra_headers['Reply-To'] reply_to = sanitize_address(reply_to, email_message.encoding) post_data.append(( 'h:Reply-To', reply_to, )) if 'X-Mailgun-Variables' in email_message.extra_headers: custom_data = email_message.extra_headers[ 'X-Mailgun-Variables'] post_data.append(('v:my-custom-data', json.dumps(custom_data))) if hasattr(email_message, 'alternatives') and email_message.alternatives: for alt in email_message.alternatives: if alt[1] == 'text/html': post_data.append(( 'html', alt[0], )) break if email_message.attachments: for attachment in email_message.attachments: post_data.append(('attachment', ( attachment[0], attachment[1], ))) content, header = encode_multipart_formdata(post_data) headers = {'Content-Type': header} else: content = post_data headers = None response = requests.post(self._api_url + "messages", auth=("api", self._access_key), data=content, headers=headers) except: if not self.fail_silently: raise return False if response.status_code != 200: if not self.fail_silently: raise MailgunAPIError(response) return False return True
def server_connect(self): while 1: print("[" + self.server_ip + "]> ", end='') self.request = input() self.request_lst = self.request.split() self.request_lst[0] = self.request_lst[0].upper() if self.request_lst[0] == "EXIT": self.server_socket.close() break # print(self.request) try: self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server_socket.connect((self.server_ip, self.server_port)) self.inout = [self.server_socket] except ConnectionRefusedError: print("\n[-] Connection Establishment Error: Could not connect to "+ self.server_ip + ":" + str(self.server_port)) return for i in range(2, len(self.request_lst)): self.request_lst[i] = self.request_lst[i].upper() if self.request_lst[0] == "GET": if len(self.request_lst) == 2: self.request_lst.append("HTTP/1.0") self.request = " ".join(self.request_lst) self.server_socket.send(self.request.encode()) self.response = self.server_socket.recv(65538) # while 1: # infds, outfds, errfds = select.select(self.inout, self.inout, [], 1) # print(len(infds), len(outfds)) # if len(infds) != 0: # self.response += self.server_socket.recv(65538) # else: # break self.response = self.response.split(b'\r\n\n',1) with open("downloads/" + self.request_lst[1], "wb")as self.f: self.f.write(self.response[1]) self.f.close() print(self.response[0]) elif self.request_lst[0] == "POST": try: with open(self.request_lst[1], "rb")as self.f: self.data = self.f.read() self.f.close() # print(self.data) from requests.packages.urllib3.filepost import encode_multipart_formdata (content, header) = encode_multipart_formdata([(self.request_lst[1], self.data)]) # print(content) # print(header) self.request = self.request.upper() self.request += "\r\n" self.request_array = bytearray() self.request_array.extend(self.request.upper().encode()) self.request_array.extend(header.encode()) self.server_socket.sendall(self.request_array) self.server_socket.sendall(content) self.response = self.server_socket.recv(65538) print("[*] "+self.response.decode()) except FileNotFoundError: print("[-] File Not Found.") else: print("[-] Unsporrted Request!") print("[-] Connection Closed")
def upload(bean, args=[{ 'content': None, 'file': None, 'mimetype': 'text/plain', 'name': 'attachment.txt' }]): from requests.packages.urllib3.fields import RequestField from requests.packages.urllib3.filepost import encode_multipart_formdata ox = bean._ox url = ox._url('attachment', 'attach') params = ox._params() meta = { 'module': bean.module_type, #'attached': bean.id, 'folder': bean.folder_id } counter = 0 fields = [] for data in args: # json metadata rf = RequestField(name='json_' + str(counter), data=json.dumps(meta)) rf.make_multipart(content_disposition='form-data') fields.append(rf) # content: data or file to read filename = 'attachment.txt' mimetype = 'text/plain' content = None if 'content' in data: content = data['content'] else: if 'file' in data: filename = data['file'] if os.path.isfile(filename): with open(filename, 'rb') as fh: content = fh.read() if content is None: #TODO: process error return None if 'name' in data: filename = data['name'] mimetype = 'text/plain' if 'mimetype' in data: mimetype = data['mimetype'] rf = RequestField(name='file_' + str(counter), data=content, filename=filename) rf.make_multipart(content_disposition='form-data', content_type=mimetype) fields.append(rf) post_body, content_type = encode_multipart_formdata(fields) content_type = ''.join(('multipart/mixed', ) + content_type.partition(';')[1:]) headers = {'Content-Type': content_type} response = requests.post(url, cookies=ox._cookies, params=params, headers=headers, data=post_body) if response and response.status_code == 200: regex = '\((\{.*\})\)' match = re.search(regex, response.content) if match: return json.loads(match.group(1)) return None
def _send(self, email_message): """A helper method that does the actual sending.""" if not email_message.recipients(): return False original_email_message = copy.deepcopy(email_message) from_email = sanitize_address(email_message.from_email, email_message.encoding) to_recipients = [ sanitize_address(addr, email_message.encoding) for addr in email_message.to ] response = None error = None try: post_data = [] post_data.append(( 'to', (",".join(to_recipients)), )) if email_message.bcc: bcc_recipients = [ sanitize_address(addr, email_message.encoding) for addr in email_message.bcc ] post_data.append(( 'bcc', (",".join(bcc_recipients)), )) if email_message.cc: cc_recipients = [ sanitize_address(addr, email_message.encoding) for addr in email_message.cc ] post_data.append(( 'cc', (",".join(cc_recipients)), )) if email_message.content_subtype == "html": post_data.append(( 'html', email_message.body, )) else: post_data.append(( 'text', email_message.body, )) post_data.append(( 'subject', email_message.subject, )) post_data.append(( 'from', from_email, )) # get our recipient variables if they were passed in recipient_variables = email_message.extra_headers.pop( 'recipient_variables', None) if recipient_variables is not None: post_data.append(( 'recipient-variables', recipient_variables, )) for name, value in self._map_smtp_headers_to_api_parameters( email_message): post_data.append(( name, value, )) if hasattr(email_message, 'alternatives') and email_message.alternatives: for alt in email_message.alternatives: if alt[1] == 'text/html': post_data.append(( 'html', alt[0], )) break # Map Reply-To header if present try: if email_message.reply_to: post_data.append(( "h:Reply-To", ", ".join(map(force_text, email_message.reply_to)), )) except AttributeError: pass if email_message.attachments: for attachment in email_message.attachments: post_data.append(('attachment', ( attachment[0], attachment[1], ))) content, header = encode_multipart_formdata(post_data) headers = {'Content-Type': header} else: content = post_data headers = None response = requests.post(self._api_url + "messages", auth=("api", self._access_key), data=content, headers=headers) except Exception, e: error = e if not self.fail_silently: raise return False
def test_length_is_correct(self): encoded = encode_multipart_formdata(self.parts, self.boundary)[0] assert len(encoded) == len(self.instance)
def to_string(self): return encode_multipart_formdata(self.fields, self.boundary_value)[0]
def _send(self, email_message): """A helper method that does the actual sending.""" if not email_message.recipients(): return False from_email = sanitize_address(email_message.from_email, email_message.encoding) recipients = [ sanitize_address(addr, email_message.encoding) for addr in email_message.recipients() ] try: post_data = [] post_data.append(( 'to', (",".join(recipients)), )) post_data.append(( 'text', email_message.body, )) post_data.append(( 'subject', email_message.subject, )) post_data.append(( 'from', from_email, )) # get our recipient variables if they were passed in recipient_variables = email_message.extra_headers.pop( 'recipient_variables', None) if recipient_variables is not None: post_data.append(( 'recipient-variables', recipient_variables, )) for name, value in self._map_smtp_headers_to_api_parameters( email_message): post_data.append(( name, value, )) if hasattr(email_message, 'alternatives') and email_message.alternatives: for alt in email_message.alternatives: if alt[1] == 'text/html': post_data.append(( 'html', alt[0], )) break # Map Reply-To header if present try: if email_message.reply_to: post_data.append(( "h:Reply-To", ", ".join(map(force_text, email_message.reply_to)), )) except AttributeError: pass if email_message.attachments: for attachment in email_message.attachments: if isinstance(attachment, MIMEBase): if "Content-ID" in attachment: post_data.append( ('inline', (attachment["Content-ID"][1:-1], attachment.get_payload(decode=True)))) elif isinstance(attachment, MIMEMessage): post_data.append( ("message", (attachment.get_filename(), attachment.get_payload(decode=True)))) else: post_data.append( ('attachment', (attachment.get_filename(), attachment.get_payload(decode=True)))) else: try: post_data.append( ('attachment', (attachment[0], attachment[1]))) except KeyError: warnings.warn( "unknown attachment: {!r}".format(attachment)) content, header = encode_multipart_formdata(post_data) headers = {'Content-Type': header} else: content = post_data headers = None response = requests.post(self._api_url + "messages", auth=("api", self._access_key), data=content, headers=headers) except: if not self.fail_silently: raise return False if response.status_code != 200: if not self.fail_silently: raise MailgunAPIError(response) return False return True
def upload(bean, args=[{'content':None,'file':None, 'mimetype':'text/plain','name':'attachment.txt'}]): from requests.packages.urllib3.fields import RequestField from requests.packages.urllib3.filepost import encode_multipart_formdata ox = bean._ox url = ox._url('attachment', 'attach') params = ox._params() meta = {'module': bean.module_type, #'attached': bean.id, 'folder': bean.folder_id} counter = 0; fields = [] for data in args: # json metadata rf = RequestField(name='json_' + str(counter) ,data=json.dumps(meta)) rf.make_multipart(content_disposition='form-data') fields.append(rf) # content: data or file to read filename = 'attachment.txt' mimetype = 'text/plain' content = None if 'content' in data: content = data['content'] else: if 'file' in data: filename = data['file'] if os.path.isfile(filename): with open(filename, 'rb') as fh: content = fh.read() if content is None: #TODO: process error return None if 'name' in data: filename = data['name'] mimetype = 'text/plain' if 'mimetype' in data: mimetype = data['mimetype'] rf = RequestField(name='file_' + str(counter), data=content, filename=filename) rf.make_multipart(content_disposition='form-data',content_type=mimetype) fields.append(rf) post_body, content_type = encode_multipart_formdata(fields) content_type = ''.join(('multipart/mixed',) + content_type.partition(';')[1:]) headers = {'Content-Type': content_type} response = requests.post(url, cookies=ox._cookies, params=params, headers=headers, data=post_body) if response and response.status_code == 200: regex='\((\{.*\})\)' match = re.search(regex, response.content) if match: return json.loads(match.group(1)) return None
def _send(self, email_message): """A helper method that does the actual sending.""" if not email_message.recipients(): return False original_email_message = copy.deepcopy(email_message) from_email = sanitize_address(email_message.from_email, email_message.encoding) to_recipients = [sanitize_address(addr, email_message.encoding) for addr in email_message.to] response = None error = None try: post_data = [] post_data.append(('to', (",".join(to_recipients)),)) if email_message.bcc: bcc_recipients = [sanitize_address(addr, email_message.encoding) for addr in email_message.bcc] post_data.append(('bcc', (",".join(bcc_recipients)),)) if email_message.cc: cc_recipients = [sanitize_address(addr, email_message.encoding) for addr in email_message.cc] post_data.append(('cc', (",".join(cc_recipients)),)) if email_message.content_subtype == "html": post_data.append(('html', email_message.body,)) else: post_data.append(('text', email_message.body,)) post_data.append(('subject', email_message.subject,)) post_data.append(('from', from_email,)) # get our recipient variables if they were passed in recipient_variables = email_message.extra_headers.pop('recipient_variables', None) if recipient_variables is not None: post_data.append(('recipient-variables', recipient_variables, )) for name, value in self._map_smtp_headers_to_api_parameters(email_message): post_data.append((name, value, )) if hasattr(email_message, 'alternatives') and email_message.alternatives: for alt in email_message.alternatives: if alt[1] == 'text/html': post_data.append(('html', alt[0],)) break # Map Reply-To header if present try: if email_message.reply_to: post_data.append(( "h:Reply-To", ", ".join(map(force_text, email_message.reply_to)), )) except AttributeError: pass if email_message.attachments: for attachment in email_message.attachments: post_data.append(('attachment', (attachment[0], attachment[1],))) content, header = encode_multipart_formdata(post_data) headers = {'Content-Type': header} else: content = post_data headers = None response = requests.post(self._api_url + "messages", auth=("api", self._access_key), data=content, headers=headers) except Exception, e: error = e if not self.fail_silently: raise return False
def _send(self, email_message): """A helper method that does the actual sending.""" if not email_message.recipients(): return False from_email = sanitize_address(email_message.from_email, email_message.encoding) recipients = [sanitize_address(addr, email_message.encoding) for addr in email_message.recipients()] try: post_data = [] post_data.append(('to', (",".join(recipients)),)) post_data.append(('text', email_message.body,)) post_data.append(('subject', email_message.subject,)) post_data.append(('from', from_email,)) # get our recipient variables if they were passed in recipient_variables = email_message.extra_headers.pop('recipient_variables', None) if recipient_variables is not None: post_data.append(('recipient-variables', recipient_variables, )) for name, value in self._map_smtp_headers_to_api_parameters(email_message): post_data.append((name, value, )) if hasattr(email_message, 'alternatives') and email_message.alternatives: for alt in email_message.alternatives: if alt[1] == 'text/html': post_data.append(('html', alt[0],)) break # Map Reply-To header if present try: if email_message.reply_to: post_data.append(( "h:Reply-To", ", ".join(map(force_text, email_message.reply_to)), )) except AttributeError: pass if email_message.attachments: for attachment in email_message.attachments: if isinstance(attachment, MIMEBase): if "Content-ID" in attachment: post_data.append(('inline', (attachment["Content-ID"][1:-1], attachment.get_payload(decode=True)))) elif isinstance(attachment, MIMEMessage): post_data.append(("message", (attachment.get_filename(), attachment.get_payload(decode=True)))) else: post_data.append(('attachment', (attachment.get_filename(), attachment.get_payload(decode=True)))) else: try: post_data.append(('attachment', (attachment[0], attachment[1]))) except KeyError: warnings.warn("unknown attachment: {!r}".format(attachment)) content, header = encode_multipart_formdata(post_data) headers = {'Content-Type': header} else: content = post_data headers = None response = requests.post(self._api_url + "messages", auth=("api", self._access_key), data=content, headers=headers) except: if not self.fail_silently: raise return False if response.status_code != 200: if not self.fail_silently: raise MailgunAPIError(response) return False return True
def _send(self, email_message): """A helper method that does the actual sending.""" if not email_message.recipients(): return False from_email = sanitize_address(email_message.from_email, email_message.encoding) to_recipients = [sanitize_address(addr, email_message.encoding) for addr in email_message.to] try: post_data = [] post_data.append(('to', (",".join(to_recipients)),)) if email_message.bcc: bcc_recipients = [sanitize_address(addr, email_message.encoding) for addr in email_message.bcc] post_data.append(('bcc', (",".join(bcc_recipients)),)) if email_message.cc: cc_recipients = [sanitize_address(addr, email_message.encoding) for addr in email_message.cc] post_data.append(('cc', (",".join(cc_recipients)),)) post_data.append(('text', email_message.body,)) post_data.append(('subject', email_message.subject,)) post_data.append(('from', from_email,)) # get our recipient variables if they were passed in recipient_variables = email_message.extra_headers.pop('recipient_variables', None) if recipient_variables is not None: post_data.append(('recipient-variables', recipient_variables, )) for name, value in self._map_smtp_headers_to_api_parameters(email_message): post_data.append((name, value, )) if hasattr(email_message, 'alternatives') and email_message.alternatives: for alt in email_message.alternatives: if alt[1] == 'text/html': post_data.append(('html', alt[0],)) break if hasattr(email_message, 'template') and email_message.template: post_data.append(('template', email_message.template,)) # not delete the 'alternatives', 'text' if hasattr(email_message,'merge_vars') and email_message.merge_vars: for k, v in email_message.merge_vars.iteritems(): post_data.append(('merge_%s'% k, v)) logger.debug(post_data) # Map Reply-To header if present try: if email_message.reply_to: post_data.append(( "h:Reply-To", ", ".join(map(force_text, email_message.reply_to)), )) except AttributeError: pass if email_message.attachments: for attachment in email_message.attachments: post_data.append(('attachment', (attachment[0], attachment[1],))) content, header = encode_multipart_formdata(post_data) headers = {'Content-Type': header, 'username': self._username, 'api_key': self._api_url } else: content = post_data headers = None logger.debug('###=======') logger.debug(content) logger.debug(headers) response = requests.post(self._api_url , auth=("api", self._api_key), data=content, headers=headers) logger.debug(response.status_code) logger.debug(response.text) email_response = response.text logger.debug('#=======') except: if not self.fail_silently: raise return False, email_response if response.status_code != 200: if not self.fail_silently: raise ElasticEmailAPIError(response) return False, email_response return True, email_response
def test_encodes_data_the_same(self): encoded = encode_multipart_formdata(self.parts, self.boundary)[0] assert encoded == self.instance.read()