使用Flask-RESTful和mod_wsgi的HTTPS

时间:2015-05-11 19:58:41

标签: python apache flask mod-wsgi google-apps

我正在尝试使用Flask-RESTful和mod_wsgi将Google Apps API Python客户端限制为HTTPS。 API本身似乎有效,但当我将Web浏览器指向HTTPS网址时,我遇到了错误。

我是Python,Flask和mod_wsgi的新手,但我有以下简化的示例代码:

/home/myself/testgoogle/testgoogle.py

#!/usr/local/bin/python
import json
import os
import sys

from DirectoryServiceObject import DirectoryServiceObject
from flask import Flask, request
from flask.ext.restful import abort, Api, Resource
from apiclient import errors
from apiclient.discovery import build

directory_service_object = DirectoryServiceObject().service_object

app = Flask( __name__ )
app.debug = True
api = Api( app )

class OrgUnitsList( Resource ):
    def get( self ):
        all_org_units = {}

        params = { "customerId": "my_customer" }

        try:
            all_org_units = directory_service_object.orgunits().list( **params ).execute()
        except errors.HttpError, e:
            error = json.loads(e.content)
            return error

        return all_org_units

api.add_resource( OrgUnitsList, "/orgunitslist" )

if __name__ == "__main__":
    app.run( host="secured.example.com", port=5001 )

/home/myself/testgoogle/testgoogle.wsgi

import sys
sys.path.insert( 0, "/home/myself/testgoogle" )
from testgoogle import app as application

/path/to/apache/ssl.conf

<VirtualHost 256.256.256.256:5001>
ServerName secured.example.com:5001

WSGIScriptAlias / /home/myself/testgoogle/testgoogle.wsgi

ErrorLog /home/myself/error.log
LogLevel warn
CustomLog /home/myself/access.log combined

<Directory /home/myself/testgoogle>
  WSGIProcessGroup testgoogle
  WSGIApplicationGroup %{GLOBAL}
  Order deny,allow
  Allow from all
</Directory>

</VirtualHost>

当我将我的网络浏览器指向https://secured.example.com:5001/orgunitslist以获取我的Google域名组织单位列表时,我发现错误&#34;无法连接到服务器&#39 ; secured.example.com&#39;&#34;

如果我第一次运行&#34; python testgoogle.py&#34; API启动,但使用Web浏览器最终会出现&#34;代码400,消息Bad request syntax&#34;,并且浏览器挂起。我假设这是因为脚本期待HTTP。当然,正如预期的那样使用HTTP工作到同一个URL,我得到一个组织单位列表。

我错过了什么?为了限制对HTTPS的API调用,我还需要做什么,或者需要做些什么呢?

1 个答案:

答案 0 :(得分:0)

我似乎通过进行以下更改来解决问题:

  • testgoogle.py已重命名为 TestGoogleClient.py
  • testgoogle.wsgi已重命名为 TestGoogleWsgi.wsgi ,我修改了最后一行以阅读from TestGoogleClient import app as application

出于某种原因,同时使用同名的.wsgi和.py文件似乎给了我&#34; app not be&#34;错误。

我还修改了我的Apache配置:

  • Listen 256.256.256.256:5001部分之外添加了WSGISocketPrefix /var/run/wsgi<VirtualHost>
  • <VirtualHost>内添加了以下内容:
    • SSLEngine on
    • SSLCertificateFile /path/to/my/cert
    • SSLCertificateKeyFile /path/to/my/key
    • WSGIDaemonProcess TestGoogleClient python-path=/path/to/python/site-packages
    • WSGIProcessGroup TestGoogleClient
    • WSGIScriptAlias / /home/myself/testgoogle/TestGoogleWsgi.wsgi

最重要的是,我需要系统管理员允许我的应用程序通过防火墙。