Python中的授权REST服务

2024-05-16 22:34:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python世界很陌生。因为我正处于设计阶段以及决定是否选择python作为实现软件的主要语言。任务是: -实现一组restfulweb服务 -将http方法授权给某些用户组,因此需要使用xacml作为策略定义(或者可以是另一个标准),而saml用于信息。交换

提前感谢您的推荐, 埃里克


Tags: 方法语言信息http标准软件定义世界
3条回答

我同意Constantinius的观点,BaseHTTPServer是在Python中实现RESTful的一个很好的方法。它预装了Python2.7,在这方面比gunicorn/flask/wsgi/gevent要好得多。并支持流式传输,您可能希望在未来的道路上。在

下面是一个示例,展示web浏览器如何对BaseHTTPServer执行远程POST调用并获取数据。在

JS(放入静态/你好.html通过Python服务):

<html><head><meta charset="utf-8"/></head><body>
Hello.

<script>

var xhr = new XMLHttpRequest();
xhr.open("POST", "/postman", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
    value: 'value'
}));
xhr.onload = function() {
  console.log("HELLO")
  console.log(this.responseText);
  var data = JSON.parse(this.responseText);
  console.log(data);
}

</script></body></html>

Python服务器(用于测试):

^{pr2}$

控制台日志(chrome):

HELLO
hello.html:14 {"req": {"value": "value"}}
hello.html:16 
{req: {…}}
req
:
{value: "value"}
__proto__
:
Object

控制台日志(firefox):

GET 
http://XXXXX:8000/hello.html [HTTP/1.0 200 OK 0ms]
POST 
XHR 
http://XXXXX:8000/postman [HTTP/1.0 200 OK 0ms]
HELLO hello.html:13:3
{"req": {"value": "value"}} hello.html:14:3
Object { req: Object }

控制台日志(边缘):

HTML1300: Navigation occurred.
hello.html
HTML1527: DOCTYPE expected. Consider adding a valid HTML5 doctype: "<!DOCTYPE html>".
hello.html (1,1)
Current window: XXXXX/hello.html
HELLO
hello.html (13,3)
{"req": {"value": "value"}}
hello.html (14,3)
[object Object]
hello.html (16,3)
   {
      [functions]: ,
      __proto__: { },
      req: {
         [functions]: ,
         __proto__: { },
         value: "value"
      }
   }

Python日志:

HTTP 8/postman[post]: PATH='/postman'
URL 8/postman[post]: request data = {"value":"value"}
URL 8/postman[post]: response data = {"req": {"value": "value"}}

此外,您还可以通过在将套接字传递给BaseHTTPServer之前包装套接字来轻松添加SSL。在

使用Python,您可能希望创建一个XACML请求,该请求将包含用户的id(您将从通常在身份验证之前发生的身份验证阶段获得),并添加有关用户所针对的WS的信息。它可能是URI,HTTP方法也是。。。在

最后,你可能会得到类似的sthg:

<?xml version="1.0" encoding="UTF-8"?><xacml-ctx:Request xmlns:xacml-ctx="urn:oasis:names:tc:xacml:2.0:context:schema:os">
   <xacml-ctx:Subject SubjectCategory="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject">
      <xacml-ctx:Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:subject:subject-id" DataType="http://www.w3.org/2001/XMLSchema#string">
         <xacml-ctx:AttributeValue>Alice</xacml-ctx:AttributeValue>
      </xacml-ctx:Attribute>
   </xacml-ctx:Subject>
   <xacml-ctx:Resource>
      <xacml-ctx:Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:resource:resource-id" DataType="http://www.w3.org/2001/XMLSchema#string">
         <xacml-ctx:AttributeValue>/someuri/myapi/target.py</xacml-ctx:AttributeValue>
      </xacml-ctx:Attribute>
   </xacml-ctx:Resource>
   <xacml-ctx:Action>
      <xacml-ctx:Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:action:action-id" DataType="http://www.w3.org/2001/XMLSchema#string">
         <xacml-ctx:AttributeValue>GET</xacml-ctx:AttributeValue>
      </xacml-ctx:Attribute>
   </xacml-ctx:Action>
   <xacml-ctx:Environment>
   </xacml-ctx:Environment>
</xacml-ctx:Request>

例如,您需要使用Python和lxml构造请求。在

反应看起来像

^{pr2}$

因此,您再次需要解析XML来提取决策,例如Permit。我为XACML-PDP编写了一个类似REST的基本接口,在这个接口中,您只需向URI发送一个httpget,将变量作为GET变量传递,例如http://www.xacml.eu/AuthZ/?a=alice&b=/someuri/myapi/target.py&c=GET

这有帮助吗?在

如果您的问题是可以使用哪些库来实现这些RESTful服务,那么请查看标准python库的BaseHTTPServer模块。在

以下代码显示了实现一个接受GET请求的简单服务器是多么容易:

class MyHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        try:
            f = open(curdir + sep + self.path) #self.path has /test.html
            self.send_response(200)
            self.send_header('Content-type',    'text/html')
            self.end_headers()
            self.wfile.write(f.read())
            f.close()
        except IOError:
            self.send_error(404,'File Not Found: %s' % self.path)

def main():
    try:
        server = HTTPServer(('', 80), MyHandler)
        print 'Welcome to the machine...'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down server'
        server.socket.close()

if __name__ == '__main__':
    main()

当然,代码不是我自己写的,我发现了here。在

相关问题 更多 >