【问题标题】:Python script as server-side for AngularJS app on IIS7Python 脚本作为 IIS7 上 AngularJS 应用程序的服务器端
【发布时间】:2015-01-19 10:36:33
【问题描述】:

我有显示播放器图像的指令。我将播放器 ID 发送到小型 Python 脚本,以通过 ajax 调用检查具有此类 ID 的图像是否存在。如果图像存在,我需要返回它的名称。

我成功地将 id 从前端发送到脚本并找到图像名称。问题是我无法正确返回文件名。我收到错误:

HTTP 错误 502.2 - 网关错误 指定的 CGI 应用程序因未返回完整的 HTTP 标头集而行为异常。它返回的标题是“HCP_23108_SmithKen.png”。

如果我添加标题,我仍然会收到错误:

HTTP 错误 502.2 - 网关错误 指定的 CGI 应用程序因未返回完整的 HTTP 标头集而行为异常。它返回的标题是“Content-type: text/html; charset=utf-8 HCP_23108_SmithKen.png”。

我确实通过关注Python on IIS: how?在 IIS7 的 Handler Mappings 中启用了 cgi

我的问题是如何在没有任何 Python 框架的情况下正确执行 Ajax GET 请求?谢谢

指令:

myDirectives.directive('headshot', function ($http) {

return {
    restrict: 'AE',
    scope: {
        lastProID: '@lastproid'
    },
    template: '<img ng-src="{{proImg}}" class="headshot">',
    link: function (scope, element, attrs) {

        //scope.id = scope.lastProID;
        attrs.$observe('lastproid', function (id) {

            scope.id = id;
            var src = 'img/bios/nophoto.png';
            scope.proImg = (src);


            var url = 'bioPhoto/bioPhoto.py';
            $http.get(
                    url,
                    {
                        params: {'id': scope.id}
                    })

                    .success(function (data) {
                        var src = 'img/bios/' + data;
                        scope.proImg = (src);
                    })
                    .error(function (error) {
                    });
        });
    }
};
});

bioPhoto.py 脚本:

import fnmatch
import os

rootPath = './img/bios/'

query_string=os.environ["QUERY_STRING"]
id=query_string.split("id=",1)[1]
id=id.strip()

pattern = "*" + id + "*"

print ('Content-type: text/html; charset=utf-8')

for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
        filename=filename.strip()
        print(filename)

【问题讨论】:

    标签: python ajax angularjs iis-7


    【解决方案1】:

    您的标题后需要一个空行。 (见:http://www.oreilly.com/openbook/cgi/ch03_02.html

    试试这个:

    print ('Content-type: text/html; charset=utf-8\n\n')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-27
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      相关资源
      最近更新 更多