def do_POST(self): content_length = int(self.headers.get("content-length")) body = self.rfile.read(content_length).decode("utf-8") try: message = json.loads(body) except json.JSONDecodeError: self.send_error(400, "JSON not parsable") return if "registration_ids" in message: recipients = message["registration_ids"] del message["registration_ids"] if "to" in message: to = message["to"] del message["to"] recipients = [to] if not recipients: self.send_error(400, "No recipients") return global output_dir for recipient in recipients: recipient = str(recipient) recipient_dir = os.path.join(output_dir, recipient) if not os.path.exists(recipient_dir): try: os.makedirs(recipient_dir) except OSError: self.send_error( 400, "Device directory [%s] could not be created" % recipient_dir, ) return try: dest = getUniqueFilename(recipient_dir, "gcm") except IOError as e: self.send_error(400, e.message) return with open(dest, "w") as f: json.dump(message, f, ensure_ascii=False) response = json.dumps({"failure": 0, "canonical_ids": 0}) self.send_response(200) self.end_headers() self.wfile.write(response.encode("utf8"))
def do_POST(self): content_length = int(self.headers.get('content-length')) body = self.rfile.read(content_length).decode('utf-8') try: message = json.loads(body) except: self.send_error(400, "JSON not parsable") return if 'registration_ids' in message: recipients = message['registration_ids'] del message['registration_ids'] if 'to' in message: to = message['to'] del message['to'] recipients = [to] if not recipients: self.send_error(400, "No recipients") return global output_dir for recipient in recipients: recipient = str(recipient) recipient_dir = os.path.join(output_dir, recipient) if not os.path.exists(recipient_dir): try: os.makedirs(recipient_dir) except OSError: self.send_error( 400, "Device directory [%s] could not be created" % recipient_dir) return try: dest = getUniqueFilename(recipient_dir, 'gcm') except IOError, e: self.send_error(400, e.message) return with codecs.open(dest, "w", encoding='utf-8') as f: f.write(json.dumps(message, ensure_ascii=False))
def do_POST(self): content_length = int(self.headers.get('content-length')) post_body = self.rfile.read(content_length) params = parse_qs(post_body) fr = params.get(b'from') to = params.get(b'to') body = params.get(b'text') if not (fr and to and body): self.send_response(400) self.end_headers() return if sys.version_info[0] >= 3: fr = fr[0].decode('utf-8') to = to[0].decode('utf-8') body = body[0].decode('utf-8') else: fr = fr[0] to = to[0] body = body[0] self.send_response(200) self.send_header("Content-type:", "text/json") self.end_headers() self.wfile.write(b'{"messages":[{"status":"0"}]}') global output_dir phone_dir = os.path.join(output_dir, to) if not os.path.exists(phone_dir): try: os.makedirs(phone_dir) except OSError: logging.error('Phone directory could not be created') return dest = getUniqueFilename(phone_dir, "sms") with open(dest, "w") as f: f.write(body) if sys.platform == 'darwin': code = body.split(None)[-1] p = subprocess.Popen('pbcopy', stdin=subprocess.PIPE) p.communicate(code.encode('utf-8')) body = '%s and has been copied to the clipboard' % body if notifier: notifier.notify(body, title=to)
def do_POST(self): content_length = int(self.headers.get("content-length")) post_body = self.rfile.read(content_length) if isinstance(post_body, bytes): post_body = post_body.decode("utf-8") params = parse_qs(post_body) fr = params.get("from") to = params.get("to") body = params.get("text") if not (fr and to and body): self.send_response(400) self.end_headers() return fr = fr[0] to = to[0] body = body[0] self.send_response(200) self.send_header("Content-type:", "text/json") self.end_headers() self.wfile.write(b'{"messages":[{"status":"0"}]}') global output_dir phone_dir = os.path.join(output_dir, to) if not os.path.exists(phone_dir): try: os.makedirs(phone_dir) except OSError: logging.error("Phone directory could not be created") return dest = getUniqueFilename(phone_dir, "sms") with open(dest, "w") as f: f.write(body) if sys.platform == "darwin": code = body.split(None)[-1] p = subprocess.Popen("pbcopy", stdin=subprocess.PIPE) p.communicate(code.encode("utf-8")) body = "%s and has been copied to the clipboard" % body if notifier: notifier.notify(body, title=to)