我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用bottle.post()。
def set_last_import_time_int(self): self.load_pkl() self.pkl["last_import_time_int"] = int(time.time()) self.dump_pkl() # pkl data format: # pkl = { # book_name: str, # total_lines: int, # last_import_time: str, # last_import_time_int: int, # } # Refer to: http://stackoverflow.com/questions/16865997/python-bottle-module-causes-error-413-request-entity-too-large # There was a bug: # If a client post something with a very large parameter, it will be encountered a "broken pipe" problem. # The line below can change the Bottle's acceptable max-size of request. # So this bug is no longer exist.
def show_post(permalink="notfound"): cookie = bottle.request.get_cookie("session") username = sessions.get_username(cookie) permalink = cgi.escape(permalink) print "about to query on permalink = ", permalink post = posts.get_post_by_permalink(permalink) if post is None: bottle.redirect("/post_not_found") # init comment form fields for additional comment comment = {'name': "", 'body': "", 'email': ""} return bottle.template("entry_template", dict(post=post, username=username, errors="", comment=comment)) # used to process a comment on a blog post
def post_comment_like(): permalink = bottle.request.forms.get("permalink") permalink = cgi.escape(permalink) comment_ordinal_str = bottle.request.forms.get("comment_ordinal") comment_ordinal = int(comment_ordinal_str) post = posts.get_post_by_permalink(permalink) if post is None: bottle.redirect("/post_not_found") return # it all looks good. increment the ordinal posts.increment_likes(permalink, comment_ordinal) bottle.redirect("/post/" + permalink)
def register(): """Send out registration email""" logger.debug(("got body: %s" % request.body.read().decode("utf-8"))) if check_if_authorized(post_get('username')): aaa.create_user(post_get('username'), 'user', post_get('password')) else: return dict( ok=False, msg="username not pre authorized" ) return 'User created' # @bottle.post('/reset_password') # def send_password_reset_email(): # """Send out password reset email""" # aaa.send_password_reset_email( # username=post_get('username'), # email_addr=post_get('email_address') # ) # return 'Please check your mailbox.' # @bottle.post('/change_password') # def change_password(): # """Change password""" # aaa.reset_password(post_get('reset_code'), post_get('password')) # return 'Thanks. <a href="/login">Go to login</a>'
def upload_view(): return """ <form action="/upload" method="post" enctype="multipart/form-data"> <input type="text" name="name" /> <input type="file" name="data" /> <input type="submit" name="submit" value="upload now" /> </form> """
def login(): return ''' <form action="/login" method="post"> Username: <input name="username" type="text" /> Password: <input name="password" type="password" /> <input value="Login" type="submit" /> </form> '''
def index(): return """ <form action="/" method="post"> Name: <input name="name" type="text" /> Alias name: <input name="alias_name" type="text" /> Tag: <input name="tag" type="text" /> <input value="Search" type="submit" /> </form> """
def test_get(self): """ WSGI: GET routes""" @bottle.route('/') def test(): return 'test' self.assertStatus(404, '/not/found') self.assertStatus(405, '/', post="var=value") self.assertBody('test', '/')
def test_post(self): """ WSGI: POST routes""" @bottle.route('/', method='POST') def test(): return 'test' self.assertStatus(404, '/not/found') self.assertStatus(405, '/') self.assertBody('test', '/', post="var=value")
def test_decorators(self): def foo(): return bottle.request.method bottle.get('/')(foo) bottle.post('/')(foo) bottle.put('/')(foo) bottle.delete('/')(foo) for verb in 'GET POST PUT DELETE'.split(): self.assertBody(verb, '/', method=verb)
def test_method_list(self): @bottle.route(method=['GET','post']) def test(): return 'ok' self.assertBody('ok', '/test', method='GET') self.assertBody('ok', '/test', method='POST') self.assertStatus(405, '/test', method='PUT')
def test_module_shortcuts(self): for name in '''route get post put delete error mount hook install uninstall'''.split(): short = getattr(bottle, name) original = getattr(bottle.app(), name) self.assertWraps(short, original)
def upload(): if request.method.lower() == "post": file = request.files.get("file") file.save("img/" + file.filename) return file.filename else: return ("<form method='post' action='/upload' enctype='multipart/form-data'>" "<input type='file' name='file'/>" "</br>" "<input type='submit'/>" "</form> ")
def admin_api(uri): def admin_api_out(func): @wraps(func) @post(uri) @require_login @json_api def func_wrapper(*args, **kwargs): if not is_admin: return {"error": "not_admin"} return func(*args, **kwargs) return admin_api_out # get salted password from request.forms
def post_new_comment(): name = bottle.request.forms.get("commentName") email = bottle.request.forms.get("commentEmail") body = bottle.request.forms.get("commentBody") permalink = bottle.request.forms.get("permalink") post = posts.get_post_by_permalink(permalink) cookie = bottle.request.get_cookie("session") username = sessions.get_username(cookie) # if post not found, redirect to post not found error if post is None: bottle.redirect("/post_not_found") return # if values not good, redirect to view with errors if name == "" or body == "": # user did not fill in enough information # init comment for web form comment = {'name': name, 'email': email, 'body': body} errors = "Post must contain your name and an actual comment." return bottle.template("entry_template", dict(post=post, username=username, errors=errors, comment=comment)) else: # it all looks good, insert the comment into the blog post and redirect back to the post viewer posts.add_comment(permalink, name, email, body) bottle.redirect("/post/" + permalink)
def post_not_found(): return "Sorry, post not found" # Displays the form allowing a user to add a new post. Only works for logged in users
def get_newpost(): cookie = bottle.request.get_cookie("session") username = sessions.get_username(cookie) # see if user is logged in if username is None: bottle.redirect("/login") return bottle.template("newpost_template", dict(subject="", body = "", errors="", tags="", username=username)) # # Post handler for setting up a new post. # Only works for logged in user.
def post_newpost(): title = bottle.request.forms.get("subject") post = bottle.request.forms.get("body") tags = bottle.request.forms.get("tags") cookie = bottle.request.get_cookie("session") username = sessions.get_username(cookie) # see if user is logged in if username is None: bottle.redirect("/login") if title == "" or post == "": errors = "Post must contain a title and blog entry" return bottle.template("newpost_template", dict(subject=cgi.escape(title, quote=True), username=username, body=cgi.escape(post, quote=True), tags=tags, errors=errors)) # extract tags tags = cgi.escape(tags) tags_array = extract_tags(tags) # looks like a good entry, insert it escaped escaped_post = cgi.escape(post, quote=True) # substitute some <p> for the paragraph breaks newline = re.compile('\r?\n') formatted_post = newline.sub("<p>", escaped_post) permalink = posts.insert_entry(title, formatted_post, tags_array, username) print "here" # now bottle.redirect to the blog permalink bottle.redirect("/post/" + permalink) # displays the initial blog signup form
def post_new_comment(): name = bottle.request.forms.get("commentName") email = bottle.request.forms.get("commentEmail") body = bottle.request.forms.get("commentBody") permalink = bottle.request.forms.get("permalink") post = posts.get_post_by_permalink(permalink) cookie = bottle.request.get_cookie("session") username = sessions.get_username(cookie) # if post not found, redirect to post not found error if post is None: bottle.redirect("/post_not_found") return # if values not good, redirect to view with errors if name == "" or body == "": # user did not fill in enough information # init comment for web form comment = {'name': name, 'email': email, 'body': body} errors = "Post must contain your name and an actual comment." return bottle.template("entry_template", {"post": post, "username": username, "errors": errors, "comment": comment}) else: # it all looks good, insert the comment into the blog post and redirect # back to the post viewer posts.add_comment(permalink, name, email, body) bottle.redirect("/post/" + permalink) # used to process a like on a blog post
def post_not_found(): return "Sorry, post not found" # Displays the form allowing a user to add a new post. Only works for # logged-in users
def get_newpost(): cookie = bottle.request.get_cookie("session") username = sessions.get_username(cookie) # see if user is logged in if username is None: bottle.redirect("/login") return bottle.template("newpost_template", {"subject": "", "body": "", "errors": "", "tags": "", "username": username}) # Post handler for setting up a new post. # Only works for logged in user.
def post_newpost(): title = bottle.request.forms.get("subject") post = bottle.request.forms.get("body") tags = bottle.request.forms.get("tags") cookie = bottle.request.get_cookie("session") username = sessions.get_username(cookie) # see if user is logged in if username is None: bottle.redirect("/login") if title == "" or post == "": errors = "Post must contain a title and blog entry" return bottle.template("newpost_template", {"subject": cgi.escape(title, quote=True), "username": username, "body": cgi.escape(post, quote=True), "tags": tags, "errors": errors}) # extract tags tags = cgi.escape(tags) tags_array = extract_tags(tags) # looks like a good entry, insert it escaped escaped_post = cgi.escape(post, quote=True) # substitute some <p> for the paragraph breaks newline = re.compile('\r?\n') formatted_post = newline.sub("<p>", escaped_post) permalink = posts.insert_entry(title, formatted_post, tags_array, username) # now bottle.redirect to the blog permalink bottle.redirect("/post/" + permalink) # displays the initial blog signup form
def postprocess_pdf(input_pdf, qr_data, qr_x=545, qr_y=20, version=None): """ PDF post-processor. Append QR code on each PDF page. :param input_pdf: PDF byte content :param qr_data: QR code data :param qr_x: X possition of QR image :param qr_y: Y possition of QR image """ qr = pyqrcode.create(qr_data, version=version) eps = StringIO() qr.eps(eps) eps.seek(0) qr_pdf = BytesIO() qr_img = Image(file=BytesIO(bytes(eps.read(), 'utf-8'))) qr_img.format = 'pdf' qr_img.save(qr_pdf) qr_page = PdfFileReader(qr_pdf).getPage(0) output_writer = PdfFileWriter() output_pdf = BytesIO() for page in PdfFileReader(BytesIO(input_pdf)).pages: page.mergeTranslatedPage(qr_page, qr_x, qr_y) output_writer.addPage(page) output_writer.write(output_pdf) output_pdf.seek(0) return output_pdf.read()
def index(): return '''<form enctype="multipart/form-data" action="./upload" method="post"> <p>File: <input type="file" name="file"></p> <p><input type="submit" value="Upload"></p> </form>'''