创建简单的python网络服务的最佳方法

问题:创建简单的python网络服务的最佳方法

我已经使用python多年了,但是我对python Web编程的经验很少。我想创建一个非常简单的Web服务,该服务公开来自现有python脚本的一些功能以供公司使用。它可能会在csv中返回结果。什么是最快的方法?如果它影响您的建议,那么我很可能会在此之后添加更多功能。

I’ve been using python for years, but I have little experience with python web programming. I’d like to create a very simple web service that exposes some functionality from an existing python script for use within my company. It will likely return the results in csv. What’s the quickest way to get something up? If it affects your suggestion, I will likely be adding more functionality to this, down the road.


回答 0

看看werkzeug。Werkzeug最初是WSGI应用程序各种实用程序的简单集合,现已成为最高级的WSGI实用程序模块之一。它包括功能强大的调试器,功能齐全的请求和响应对象,用于处理实体标签的HTTP实用程序,高速缓存控制标头,HTTP日期,cookie处理,文件上传,功能强大的URL路由系统和一堆由社区提供的附加模块。

它包括许多可与http配合使用的出色工具,并且具有可以在不同环境(cgi,fcgi,apache / mod_wsgi或用于调试的普通简单python服务器)中与wsgi一起使用的优点。

Have a look at werkzeug. Werkzeug started as a simple collection of various utilities for WSGI applications and has become one of the most advanced WSGI utility modules. It includes a powerful debugger, full featured request and response objects, HTTP utilities to handle entity tags, cache control headers, HTTP dates, cookie handling, file uploads, a powerful URL routing system and a bunch of community contributed addon modules.

It includes lots of cool tools to work with http and has the advantage that you can use it with wsgi in different environments (cgi, fcgi, apache/mod_wsgi or with a plain simple python server for debugging).


回答 1

web.py可能是最简单的Web框架。“裸” CGI比较简单,但是在提供一项实际上可以做某事的服务时,您完全是一个人。

“你好,世界!” 根据web.py是不是比裸CGI版本更长的时间,但它增加了URL映射,HTTP命令的区别,和查询参数解析为免费

import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:        
    def GET(self, name):
        if not name: 
            name = 'world'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

web.py is probably the simplest web framework out there. “Bare” CGI is simpler, but you’re completely on your own when it comes to making a service that actually does something.

“Hello, World!” according to web.py isn’t much longer than an bare CGI version, but it adds URL mapping, HTTP command distinction, and query parameter parsing for free:

import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:        
    def GET(self, name):
        if not name: 
            name = 'world'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

回答 2

在线获取Python脚本的最简单方法是使用CGI:

#!/usr/bin/python

print "Content-type: text/html"
print

print "<p>Hello world.</p>"

将该代码放入驻留在Web服务器CGI目录中的脚本中,使其可执行并运行。cgi当您需要接受用户的参数时,该模块具有许多有用的实用程序。

The simplest way to get a Python script online is to use CGI:

#!/usr/bin/python

print "Content-type: text/html"
print

print "<p>Hello world.</p>"

Put that code in a script that lives in your web server CGI directory, make it executable, and run it. The cgi module has a number of useful utilities when you need to accept parameters from the user.


回答 3

原始CGI有点痛苦,Django有点重量级。关于它,有许多更简单,更轻便的框架,例如CherryPy。值得一看。

Raw CGI is kind of a pain, Django is kind of heavyweight. There are a number of simpler, lighter frameworks about, e.g. CherryPy. It’s worth looking around a bit.


回答 4

看一下WSGI参考实现。您已经在Python库中拥有它。这很简单。

Look at the WSGI reference implementation. You already have it in your Python libraries. It’s quite simple.


回答 5

如果您指的是“ Web服务”,则其他程序SimpleXMLRPCServer可能访问了某些 适合您的东西。自版本2.2起,所有Python安装中均包含该功能。

对于简单的人类可访问的东西,我通常使用Python的SimpleHTTPServer,它也随每次安装一起提供。显然,您还可以通过客户端程序访问SimpleHTTPServer。

If you mean with “Web Service” something accessed by other Programms SimpleXMLRPCServer might be right for you. It is included with every Python install since Version 2.2.

For Simple human accessible things I usually use Pythons SimpleHTTPServer which also comes with every install. Obviously you also could access SimpleHTTPServer by client programs.


回答 6

如果您拥有一个好的Web框架,生活将会很简单。Django中的Web服务很简单。定义模型,编写返回CSV文档的视图函数。跳过模板。

Life is simple if you get a good web framework. Web services in Django are easy. Define your model, write view functions that return your CSV documents. Skip the templates.


回答 7

如果您用SOAP / WSDL表示“ Web服务”,则可能需要看一下 使用Python和SOAPpy生成WSDL

If you mean “web service” in SOAP/WSDL sense, you might want to look at Generating a WSDL using Python and SOAPpy


回答 8

也许是扭曲的 http://twistedmatrix.com/trac/