【问题标题】:HTTPS with Flask-RESTful and mod_wsgi带有 Flask-RESTful 和 mod_wsgi 的 HTTPS
【发布时间】:2015-05-11 19:58:40
【问题描述】:

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

我对 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 域的组织单位列表时,出现错误“无法连接到服务器 'secured.example.com'”。

如果我首先运行“python testgoogle.py”,API 会启动,但使用 Web 浏览器会以“代码 400,消息错误请求语法”结束,并且浏览器会挂起。我假设这是因为脚本需要 HTTP。当然,正如预期的那样,使用 HTTP 访问相同的 URL 是可行的,并且我得到了组织单位的列表。

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

【问题讨论】:

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


    【解决方案1】:

    我似乎通过以下更改解决了这个问题:

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

    出于某种原因,同时拥有同名的 .wsgi 和 .py 文件似乎会给我“找不到应用程序”错误。

    我还修改了我的 Apache 配置:

    • &lt;VirtualHost&gt; 部分之外添加了Listen 256.256.256.256:5001WSGISocketPrefix /var/run/wsgi
    • &lt;VirtualHost&gt; 中添加了以下内容:
      • 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

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

    【讨论】:

      猜你喜欢
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 2020-01-26
      • 1970-01-01
      • 2015-12-21
      • 2020-04-27
      • 1970-01-01
      相关资源
      最近更新 更多