简体   繁体   English

关于制作网络应用程序的初学者Python问题

[英]Beginner Python question about making a web app

I am completely new to Python-- never used it before today. 我是Python的新手 - 从未在今天之前使用它。 I am interested in devloping Python applications for the web. 我感兴趣的是为Web开发Python应用程序。 I would like to check to see if my web server supports WSGI or running python apps in some way. 我想检查一下我的Web服务器是否支持WSGI或以某种方式运行python应用程序。

Let's say I have a .py file that prints "Hello world!". 假设我有一个打印“Hello world!”的.py文件。 How can I test to see if my server supports processing this file? 如何测试我的服务器是否支持处理此文件?

FYI, this is a Mac OS X server 10.5. 仅供参考,这是一台Mac OS X服务器10.5。 So I know Python is installed (It's installed on Mac OS X by default), but I don't know if it's set up to process .py files server-side and return the results. 所以我知道安装了Python(它默认安装在Mac OS X上),但我不知道它是否设置为处理服务器端的.py文件并返回结果。

BTW, I'm coming from a PHP background, so this is a bit foreign to me. 顺便说一句,我来自PHP背景,所以这对我来说有点陌生。 I've looked at the python docs re: wgsi, cgi, etc. but since I haven't done anything concrete yet, it's not quite making sense. 我看过python docs re:wgsi,cgi等等,但由于我还没有做任何具体的事情,所以它并没有多大意义。

A very basic WSGI application can look as follows: 一个非常基本的WSGI应用程序可以如下所示:

def application(environ, start_response):
    start_response('200 OK', [('content-type', 'text/html')])
    return ['Hello world!']

Unfortunately, if you put this into helloworld.py on the server and then browse to URL/helloworld.py you will most likely see the code. 不幸的是,如果你把它放在服务器上的helloworld.py然后浏览到URL / helloworld.py,你很可能会看到代码。

In general you need to add very specific configuration options to the server (or to a server configuration file) to get it to serve your python 'application' correctly. 通常,您需要向服务器(或服务器配置文件)添加非常具体的配置选项,以使其正确地为您的python“应用程序”提供服务。 Given you are using mod_wsgi on Apache 2, a configuration could look as follows: 鉴于您在Apache 2上使用mod_wsgi,配置可能如下所示:

<VirtualHost *>
    ServerName example.com
    WSGIScriptAlias /server/location/address /path/to/helloworld.py
</VirtualHost>

Where /server/location/address is the endpoint of the URL you have to browse to. 其中/ server / location / address是您必须浏览到的URL的端点。

This is because python WSGI catches all URLs passed to it, and pushes them to the same entry point (your application method/class). 这是因为python WSGI捕获传递给它的所有URL,并将它们推送到相同的入口点(您的应用程序方法/类)。 And from the information received in the parameters, the application must decide what page to return. 并且从参数中收到的信息中,应用程序必须决定返回哪个页面。

Since this information is so application specific there 'should' be a way to configure it on the server, however I have yet to come across a web-hosting configuration panel that allows the configuration of Python applications. 由于此信息是特定于应用程序的,因此应该有一种在服务器上配置它的方法,但是我还没有遇到允许配置Python应用程序的Web托管配置面板。 This generally means you have to contact the server administrators and have them configure it for you. 这通常意味着您必须联系服务器管理员并让他们为您配置它。

However, in general, when you sign up for hosting the company generally has a page where they tell you exactly what is supported on their servers (generally: php, mysql) and how much space and bandwidth you are allowed. 但是,一般情况下,当您注册托管时,公司通常会有一个页面,告诉您他们服务器上支持的内容(通常是:php,mysql)以及允许的空间和带宽。 Thus if they do not list it on their site, it is highly likely they will not support it. 因此,如果他们没有在他们的网站上列出,很可能他们不会支持它。

To get around this you can instead buy a VPS (Virtual Private Server) and then configure it however you want. 为了解决这个问题,您可以购买VPS(虚拟专用服务器),然后根据需要进行配置。

If you are new to Python and Python web application development, then ignore all the hosting issues to begin with and don't start from scratch. 如果您不熟悉Python和Python Web应用程序开发,那么请忽略所有托管问题并且不要从头开始。 Simply go get a full featured Python web framework such as Django or web2py and learn how to write Python web applications using their in built development web server. 只需获得一个功能齐全的Python Web框架,如Django或web2py,并学习如何使用他们内置的开发Web服务器编写Python Web应用程序。 You will only cause yourself much pain by trying to solve distinct problem of production web hosting first. 通过尝试首先解决生产网络托管的明显问题,你只会给自己带来很多痛苦。

Read this first. 先读一下。

How Python web frameworks, WSGI and CGI fit together Python Web框架,WSGI和CGI如何组合在一起

Then revise your question to specifically call out things you haven't found out yet. 然后修改你的问题,专门调出你还没有发现的东西。

After reading this, reread the part about mod_wsgi being one of several ways Apache can run Python. 阅读本文之后,重新阅读关于mod_wsgi的部分,这是Apache可以运行Python的几种方式之一。

After rereading that part, reread the part about how Python can be run as a cgi script. 重读该部分后,重新阅读有关Python如何作为cgi脚本运行的部分。 This is another of several ways Apache can run Python. 这是Apache可以运行Python的几种方式中的另一种。

After rereading all of that, please revise the question to explain what part of the Apache configuration confuses you. 重新阅读完所有内容后,请修改问题以解释Apache配置的哪些部分让您感到困惑。

Can you not find mod_wsgi ? 你能找到mod_wsgi吗? Can you not find mod_cgi ? 你能找到mod_cgi吗? Please be specific on what parts of Apache configuration confuse you. 请具体说明Apache配置的哪些部分让您感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM