正在显示
1 个修改的文件
包含
31 行增加
和
15 行删除
| @@ -36,25 +36,14 @@ reload(sys); | @@ -36,25 +36,14 @@ reload(sys); | ||
| 36 | exec("sys.setdefaultencoding('utf-8')"); | 36 | exec("sys.setdefaultencoding('utf-8')"); |
| 37 | assert sys.getdefaultencoding().lower() == "utf-8"; | 37 | assert sys.getdefaultencoding().lower() == "utf-8"; |
| 38 | 38 | ||
| 39 | -if __name__ != "__main__": | ||
| 40 | - raise Exception("embed not support"); | ||
| 41 | - | ||
| 42 | -if len(sys.argv) <= 1: | ||
| 43 | - print "SRS api callback server, Copyright (c) 2013 winlin" | ||
| 44 | - print "Usage: python %s <port>"%(sys.argv[0]) | ||
| 45 | - print " port: the port to listen at." | ||
| 46 | - print "For example:" | ||
| 47 | - print " python %s 8085"%(sys.argv[0]) | ||
| 48 | - print "" | ||
| 49 | - print "See also: https://github.com/winlinvip/simple-rtmp-server" | ||
| 50 | - sys.exit(1) | ||
| 51 | - | ||
| 52 | import json, datetime, cherrypy | 39 | import json, datetime, cherrypy |
| 53 | 40 | ||
| 41 | +# simple log functions. | ||
| 54 | def trace(msg): | 42 | def trace(msg): |
| 55 | date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | 43 | date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 56 | print "[%s][trace] %s"%(date, msg) | 44 | print "[%s][trace] %s"%(date, msg) |
| 57 | 45 | ||
| 46 | +# enable crossdomain access for js-client | ||
| 58 | def enable_crossdomain(): | 47 | def enable_crossdomain(): |
| 59 | cherrypy.response.headers["Access-Control-Allow-Origin"] = "*" | 48 | cherrypy.response.headers["Access-Control-Allow-Origin"] = "*" |
| 60 | cherrypy.response.headers["Access-Control-Allow-Methods"] = "GET, POST, HEAD, PUT, DELETE" | 49 | cherrypy.response.headers["Access-Control-Allow-Methods"] = "GET, POST, HEAD, PUT, DELETE" |
| @@ -62,6 +51,7 @@ def enable_crossdomain(): | @@ -62,6 +51,7 @@ def enable_crossdomain(): | ||
| 62 | allow_headers = ["Cache-Control", "X-Proxy-Authorization", "X-Requested-With", "Content-Type"] | 51 | allow_headers = ["Cache-Control", "X-Proxy-Authorization", "X-Requested-With", "Content-Type"] |
| 63 | cherrypy.response.headers["Access-Control-Allow-Headers"] = ",".join(allow_headers) | 52 | cherrypy.response.headers["Access-Control-Allow-Headers"] = ",".join(allow_headers) |
| 64 | 53 | ||
| 54 | +# error codes definition | ||
| 65 | class Error: | 55 | class Error: |
| 66 | # ok, success, completed. | 56 | # ok, success, completed. |
| 67 | success = 0 | 57 | success = 0 |
| @@ -74,12 +64,14 @@ POST: create new client, handle the SRS on_connect callback. | @@ -74,12 +64,14 @@ POST: create new client, handle the SRS on_connect callback. | ||
| 74 | ''' | 64 | ''' |
| 75 | class RESTClients(object): | 65 | class RESTClients(object): |
| 76 | exposed = True | 66 | exposed = True |
| 67 | + | ||
| 77 | def GET(self): | 68 | def GET(self): |
| 78 | enable_crossdomain(); | 69 | enable_crossdomain(); |
| 79 | 70 | ||
| 80 | clients = {}; | 71 | clients = {}; |
| 81 | return json.dumps(clients); | 72 | return json.dumps(clients); |
| 82 | 73 | ||
| 74 | + # for SRS hook: on_connect | ||
| 83 | def POST(self): | 75 | def POST(self): |
| 84 | enable_crossdomain(); | 76 | enable_crossdomain(); |
| 85 | 77 | ||
| @@ -97,21 +89,43 @@ class RESTClients(object): | @@ -97,21 +89,43 @@ class RESTClients(object): | ||
| 97 | def OPTIONS(self): | 89 | def OPTIONS(self): |
| 98 | enable_crossdomain() | 90 | enable_crossdomain() |
| 99 | 91 | ||
| 92 | +# HTTP RESTful path. | ||
| 100 | class Root(object): | 93 | class Root(object): |
| 101 | def __init__(self): | 94 | def __init__(self): |
| 102 | self.api = Api() | 95 | self.api = Api() |
| 103 | - | 96 | +# HTTP RESTful path. |
| 104 | class Api(object): | 97 | class Api(object): |
| 105 | def __init__(self): | 98 | def __init__(self): |
| 106 | self.v1 = V1() | 99 | self.v1 = V1() |
| 107 | - | 100 | +# HTTP RESTful path. to access as: |
| 101 | +# http://127.0.0.1:8085/api/v1/clients | ||
| 108 | class V1(object): | 102 | class V1(object): |
| 109 | def __init__(self): | 103 | def __init__(self): |
| 110 | self.clients = RESTClients() | 104 | self.clients = RESTClients() |
| 111 | 105 | ||
| 106 | +''' | ||
| 107 | +main code start. | ||
| 108 | +''' | ||
| 109 | +# donot support use this module as library. | ||
| 110 | +if __name__ != "__main__": | ||
| 111 | + raise Exception("embed not support"); | ||
| 112 | + | ||
| 113 | +# check the user options | ||
| 114 | +if len(sys.argv) <= 1: | ||
| 115 | + print "SRS api callback server, Copyright (c) 2013 winlin" | ||
| 116 | + print "Usage: python %s <port>"%(sys.argv[0]) | ||
| 117 | + print " port: the port to listen at." | ||
| 118 | + print "For example:" | ||
| 119 | + print " python %s 8085"%(sys.argv[0]) | ||
| 120 | + print "" | ||
| 121 | + print "See also: https://github.com/winlinvip/simple-rtmp-server" | ||
| 122 | + sys.exit(1) | ||
| 123 | + | ||
| 124 | +# parse port from user options. | ||
| 112 | port = int(sys.argv[1]) | 125 | port = int(sys.argv[1]) |
| 113 | trace("api server listen at port: %s"%(port)) | 126 | trace("api server listen at port: %s"%(port)) |
| 114 | 127 | ||
| 128 | +# cherrypy config. | ||
| 115 | conf = { | 129 | conf = { |
| 116 | 'global': { | 130 | 'global': { |
| 117 | 'server.shutdown_timeout': 1, | 131 | 'server.shutdown_timeout': 1, |
| @@ -121,9 +135,11 @@ conf = { | @@ -121,9 +135,11 @@ conf = { | ||
| 121 | 'tools.encode.encoding': "utf-8" | 135 | 'tools.encode.encoding': "utf-8" |
| 122 | }, | 136 | }, |
| 123 | '/': { | 137 | '/': { |
| 138 | + # for cherrypy RESTful api support | ||
| 124 | 'request.dispatch': cherrypy.dispatch.MethodDispatcher() | 139 | 'request.dispatch': cherrypy.dispatch.MethodDispatcher() |
| 125 | } | 140 | } |
| 126 | } | 141 | } |
| 127 | 142 | ||
| 143 | +# start cherrypy web engine | ||
| 128 | trace("start cherrypy server") | 144 | trace("start cherrypy server") |
| 129 | cherrypy.quickstart(Root(), '/', conf) | 145 | cherrypy.quickstart(Root(), '/', conf) |
-
请 注册 或 登录 后发表评论