1. 浏览器端:
在发送跨域请求时,如:PUT http://cat:1974/api/v1/sources/16162
浏览器会自动将同样的参数以OPTIONS的请求方式对服务器进行请求:OPTIONS http://cat:1974/api/v1/sources/16162
当得到服务器端进行允许跨域的响应之后,才会按照我们指定的方式进行请求,如下图:
2. 服务器端(cherrypy):
假设服务器端有如下的可被访问的资源:
class sources:
exposed = True
def OPTIONS():
# 跨域处理
def PUT(id):
# 跨域处理
# do something
def GET(start, count):
# 跨域处理
# do something
cherrypy会对跨域请求的url进行一系列的前处理,获取到所请求的方法名以及参数,找到相匹配的方法进行分配
如上中举的例子,当cherrypy获取到请求OPTIONS http://cat:1974/api/v1/sources/16162时,
它会根据url所指定的请求资源(sources),方法(OPTIONS),以及相应参数(start),找到相对应的方法,
但是由于相应的OPTIONS方法参数列表为空,与请求的参数start不匹配,所以cherrypy认为没有合适的资源,直接响应404,如下图:
2.1 所以使用cherrypy实现rest api跨域时,需要考虑到参数列表的情况,
上面的情况可以将OPTIONS和GET的参数列表设为一样即可:
def OPTIONS(start)
2.2 但是这样只能解决带有一个参数的请求跨域的情况,
如果浏览器发送的是GET http://cat:1974/api/v1/sources/0/10 这样的多参数的请求,则会有同样的问题,
这里可以用到python的可变参数*arg:
def OPTIONS(*arg)
这样的话,不管OPTION有多少个参数,都能请求成功了
2.3 另外,上面举的例子都是rest风格的url,如果换为标准的HTTP GET风格的url就需要使用**kwargs了,
如GET http://cat:1974/api/v1/sources?start=0&count=10
3. 最后的解决方案:
def OPTIONS(*args, **kwargs)
1. 浏览器端:在发送跨域请求时,如:PUT http://cat:1974/api/v1/sources/16162浏览器会自动将同样的参数以OPTIONS的请求方式对服务器进行请求:OPTIONS http://cat:1974/api/v1/sources/16162当得到服务器端进行允许跨域的响应之后,才会按照我们指定的方式进行请求,如下图:2. 服务器端(cher
django 的并发能力真的是令人担忧,django本身框架下只有一个线程在处理请求,任何一个请求阻塞,就会影响另一个情感求的响应,尤其是涉及到IO操作时,基于框架下开发的视图的响应并没有对应的开启多线程,再者
Python
的多线程对于多核CPU有效利用率非常低,参照
这里就使用 nginx + uwsgi 提供高并发
nginx 的并发能力超高,单台并发能力过万(这个也不是绝对),在纯静态的 ...
#cd srs/trunk
#vi src/app/srs_app_http_stream.cpp
在w->header()->set_content_type(“video/x-flv”);下一行添加代码
w->header()-&am
Over the last few years, the boom that the World has experienced with the Internet
breakthrough has pushed almost every programming language or platform to
welcome the rise of web development toolkits, libraries, and frameworks.
The
Python
programming language has grown a rather large list of these
environments though apart from a few of them such as Zope and Twisted most
have a fairly small community. It is in this context that
Cherry
Py
came into existence
when Rémi Delon, its creator, decided that he needed a tool that would work as
he wanted for his own personal projects. He then released
Cherry
Py
under a free
software license so that anyone could use, distribute, and contribute to the project.
Cherry
Py
is a
Python
library implementing the HTTP protocol, which is at the very
core of the Web, using common
Python
idioms. On top of that
Cherry
Py
offers its
own view and concepts on how to help a developer to build web applications while
being minimally intrusive through its own simple and straightforward API.
This book will guide you through the
Cherry
Py
library with the aim of giving you
the key to make the best of it in your own web applications.
The first four chapters are dedicated to
Cherry
Py
, providing information ranging
from its history to an in-depth presentation of its key features. The rest of the book
will then take you into the development of a photoblog application. Each chapter
tries to provide enough background to allow you to ponder the why and how of
each decision made. Indeed writing software applications is not a precise science and
compromises need to be undertaken for the better, however, the truth is that writing
software usually does not go quite as planned. I have written this book with the hope
that in the end you would have learnt much more than using a
Python
library.
Preface
[ 2 ]
What This Book Covers
Chapter 1 presents the story behind
Cherry
Py
and a high-level overview of the project.
Chapter 2 guides you through the installation and deployment of
Cherry
Py
via
common strategies like using distutils, setuptools, or subversion.
Chapter 3 gives an overview of the main and the most common aspects of
Cherry
Py
,
which will give you an understanding of what the library can do.
Chapter 4 goes into an in-depth review of the main aspects of the library such as its
support for the HTTP protocol or the WSGI interface. It also extensively discusses the
tool feature of the
Cherry
Py
API.
Chapter 5 introduces the application, which will be the unifying theme for the rest of
the book. The chapter reviews the basic entities that the application will manipulate
before moving onto explaining how we will map them into a relational database.
This will allow us to explain the concept of ORM and perform a quick comparison
between SQLAlchemy, SQLObject, and Dejavu.
Chapter 6 presents the idea behind web services by reviewing REST and the Atom
Publishing Protocol.
Chapter 7 describes how to use a templating engine such as Kid to generate web
pages dynamically. The chapter also introduces Mochikit a JavaScript toolkit to
perform client-side development.
Chapter 8 extends chapter 7 by diving into the world of Ajax, which has reminded
web developers that they can create extremely powerful applications by simply
using the browser capabilities, the JavaScript language, and the HTTP protocol.
Chapter 9 makes a strong point that any application should be reasonably well tested
and introduces some testing strategies like unit testing, functional testing, and
load testing.
Chapter 10 ends the book by reviewing some methods to deploy a
Cherry
Py
application under a common web-server front end like Apache and lighttpd. The
chapter also explains how to enable SSL from your
Cherry
Py
application.
先安装
cherry
Py
模块,pip3 install
cherry
Py
-i https://
py
pi.tuna.tsinghua.edu.cn/simple
在
py
charm里建个文件
import sys
sys.path.append("./cheery")
import
cherry
py
class HelloWord(object):
@
cherry
py
.expose
跨域
服务器
不支持
跨域
访问的 Web API 服务器。 提供两个不同版本的服务器代码(
python
和服务器端 javascript,通过 node.js)。
Python
版
服务器的
python
版本基于的服务器,使用
cherry
py
。
python
server.
py
##Node 版本 node 版本需要 http、url 和 httpdispatcher。
node server.js
py
mortgage 应用程序是一个能够动态比较多种不同类型抵押贷款的应用程序。 有很多不同的抵押贷款计算器,但我发现没有一个可以让您像这样进行细致的比较。
项目源在托管,我部署了一个开发版本,因为我在 Openshift 上进行了重大更改 。 请随时访问并测试它!
有关最新版本、贡献以及更多信息,请访问 GitHub 上的项目页面或 Openshift 节点。
使用的技术
我使用这个项目通过 Bootstrap 和 D3 图表库来学习 HTML/CSS/JS。 除此之外,我还通过这个项目增加了对
Python
的理解,并了解了一些 Openshift 及其工作原理。 后端 API 是
python
,它为前端 UI 生成数据以进行图表和显示。
请通过在打开问题来报告任何错误或请求的增强功能。
如果你想在本地运行它,克隆当前的主(开发):
git clo
最近几天在看Cherr
Py
,中文资料很少所以打算看文档的时候顺便翻译下来,希望能给需要的朋友一点帮助。
前面几节已经有中文版了,附上连接点击打开链接
这里从Basics这一章开始
一分钟的应用实例:
你可以使用
Cherry
Py
编写的最基本的web应用程序,几乎涉及其所有核心概念。
import
cherry
py
class Root(object):
@
cherry
Python
有很多不同的 Web 框架可供选择。以下是一些最受欢迎的
Python
Web 框架:
1. Django:Django 是
Python
最流行的 Web 框架之一,用于开发复杂的 Web 应用程序。它有一个庞大的社区,提供了许多功能强大的工具和库,如 ORM、表单处理和安全性。
2. Flask:Flask 是一个轻量级 Web 框架,简单易用,适用于开发小型 Web 应用程序和 API。它具有灵活性和可扩展性,可以根据需求选择添加需要的插件和库。
3.
Py
ramid:
Py
ramid 是一个功能强大的 Web 框架,专注于灵活性和可扩展性。它具有大量的插件和库,适用于开发大型和复杂的 Web 应用程序。
4. Bottle:Bottle 是一个轻量级 Web 框架,代码简单易懂,适用于开发小型 Web 应用程序和 API。它内置了一个简单的模板引擎,可以轻松处理 HTTP 请求和响应。
5.
Cherry
Py
:
Cherry
Py
是一个 Web 框架,可以帮助开发人员构建高性能的 Web 应用程序和服务。它可以直接运行在多种服务器上,例如 Apache、IIS 和 Nginx。
以上是一些最流行的
Python
Web 框架,每个框架都有其独特的特点和优势,可以根据自己的需求和项目的要求选择合适的框架。