Cherrypy在Python Web项目中如何使用

  

Cherrypy在Python Web项目中如何使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

<强> 1,介绍

搭建Java Web项目,需要Tomcat服务器才能进行。而搭建Python Web项目,因为Cherrypy自带服务器,所以只需要下载该模块就能进行网络项目开发。

<强> 2,最基本用法

实现功能:访问html页面,点击按钮后接收后台py返回的值

html页面(test_cherry。html)

& lt; !DOCTYPE html>   & lt; html>      & lt; head>   & lt;元charset=皍tf-8"祝辞   & lt; title>测试Cherry   https://www.yisu.com/zixun/& lt;脚本src=" https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js ">      

测试樱桃

  

  <按钮类型=鞍磁ァ?=utf - 8 - * - # - * -编码      进口cherrypy         类TestCherry ():   @cherrypy.expose() #保证html能请求到该函数   def hello_world(自我):   打印(& # 39;你好# 39;)   返回& # 39;你好& # 39;      @cherrypy.expose() #保证html能请求到该函数http://127.0.0.1:8080指数   def指数(自我):#默认页为test_cherry.html   返回打开(u # 39; test_cherry.html& # 39;)         cherrypy.quickstart (TestCherry () & # 39;/& # 39;)

运行结果

[27/可能/2020:09:04:42]引擎监听SIGTERM。
[27/可能/2020:09:04:42]引擎巴士开始
CherryPy检查:
应用程序安装在& # 39;& # 39;有一个空的配置。

[27/可能/2020:09:04:42]引擎设置事件处理程序控制台。
[27/可能/2020:09:04:42]引擎开始监视线程& # 39;Autoreloader& # 39;。
[27/可能/2020:09:04:42]引擎服务alt=" Cherrypy在Python Web项目中如何使用“>

点击hello_world按钮,就会访问py中的hello_world函数

 Cherrypy在Python Web项目中如何使用“> </p> <p>解释:test_cherry。html中</p> <blockquote> <p>函数callHelloWorld () {</p> <p> $ . get (& # 39;/hello_world # 39;功能(数据、状态){</p> <p>警报(& # 39;数据:& # 39;+数据)</p> <p> <em>警报(& # 39;状态:& # 39;+状态)</em> </p> <p> <em> <em> <em> <em> <em> <em>}) </em> </em> </em> </em> </em> </em> <em> <em> <em> <em> <em> <em>} </em> </em> </em> </em> </em> </em> </p> </引用> <p> 1)请求/hello_world需要与py中的函数名一致</p> <p> 2)默认端口是8080,如果8080年被占用,可以重新配置</p> <p> cherrypy.quickstart (TestCherry() & # 39;/& # 39;)可以接收配置参数</p> <p>若多次调试出现预示。超时:8080端口不自由>=utf - 8 - * - # - * -编码
  
  进口cherrypy
  导入web浏览器
  
  
  类TestCherry ():
  @cherrypy.expose() #保证html能请求到该函数
  def hello_world(自我):
  打印(& # 39;你好# 39;)
  返回& # 39;你好& # 39;
  
  @cherrypy.expose() #保证html能请求到该函数http://127.0.0.1:8080指数
  def指数(自我):#默认页为test_cherry.html
  返回打开(u # 39; test_cherry.html& # 39;)
  
  def auto_open ():
  webbrowser.open (& # 39; http://127.0.0.1:8080 & # 39;)
  
  cherrypy.engine.subscribe(& # 39;开始# 39;,auto_open) #启动前每次都调用auto_open函数
  cherrypy.quickstart (TestCherry () & # 39;/& # 39;) </pre> <p>这样运行py就能自动打开网页了,每次改变html代码如果没达到预期效果,可以试一试清理浏览器缓存! ! ! </p> <p> 4,带参数的请求</p> <p>实现传入参数并接收返回显示在html上</p> <p> py中添加一个函数(get_parameters) </p> <pre class==utf - 8 - * - # - * -编码      进口cherrypy   导入web浏览器         类TestCherry ():   @cherrypy.expose() #保证html能请求到该函数   def hello_world(自我):   打印(& # 39;你好# 39;)   返回& # 39;你好& # 39;      @cherrypy.expose() #保证html能请求到该函数http://127.0.0.1:8080指数   def指数(自我):#默认页为test_cherry.html   返回打开(u # 39; test_cherry.html& # 39;)   @cherrypy.expose ()   def get_parameters(自我,名字、年龄、* * kwargs):   打印(& # 39;名称:{}& # 39;.format(名字))   打印(& # 39;年龄:{}& # 39;.format(年龄))   打印(& # 39;kwargs: {} & # 39; .format (kwargs))   成功返回& # 39;得到参数# 39;   def auto_open ():   webbrowser.open (& # 39; http://127.0.0.1:8080 & # 39;)   cherrypy.engine.subscribe(& # 39;开始# 39;,auto_open) #启动前每次都调用auto_open函数   cherrypy.quickstart (TestCherry () & # 39;/& # 39;)

Cherrypy在Python Web项目中如何使用