Nginx系列1.2:nginx-rtmp流媒體服務器添加權限認證(推流權限和播放權限)


用到的工具:OBS Studio(推流)、nginx-rtmp流媒體服務器VLC(拉取流播放)

Nginx系列1:ubuntu16.04編譯出適合自己的nginx服務器

Nginx系列1.1:ubuntu16.04編譯nginx-rtmp流媒體服務器

1 配置推流權限

配置nginx-rtmp流媒體服務器的conf/nginx.conf文件;

rtmp {
     server {
         listen 1935; #監聽的端口 chunk_size 4000; application hls { #rtmp推流請求路徑 live on; hls on; hls_path /home/zopen/nginx/hls; hls_fragment 5s; on_publish http://localhost:10078/user/auth; # notify_method get; # on_publish http://localhost:81/on_publish.php;  } }

把auth.py放到任意目錄下面,運行:

(固定用戶名密碼驗證和數據庫驗證二選一即可)

auth.py代碼(使用固定用戶名密碼):

#!/usr/bin/env python
# -*- coding:utf-8 -*- #https://www.jianshu.com/p/0d14ae8f081c  #https://blog.csdn.net/wei389083222/article/details/78721074/ #https://blog.csdn.net/weixin_34368949/article/details/85991563 #驗證只能用post方法 #雖然流密鑰的格式像是get類型,但是必須使用POST獲取參數。 from flask import Flask, request, Response app = Flask(__name__) #傳入url格式為: xx.xx.xx.xx:10078/user/auth?usr=xxx&passWord=xxx @app.route('/user/auth',methods=['POST']) def auth(): usr = request.form['username']#從url后獲取的數據 password = request.form['password'] print(user,'\t',password) #此處可改為從數據庫獲取數據 auth_user='abc' auth_passWord='123' if auth_user == user and auth_passWord ==password: return Response(response='success',status=200)#返回200狀態碼 else: return Response(status=500)#返回500狀態碼 #Flask.abort(404) return password if __name__ == '__main__': app.run(host='0.0.0.0',port=10078,debug=True)

auth.py代碼(使用數據庫驗證):

#!/usr/bin/python3
# -*- coding:utf-8 -*- #https://www.jianshu.com/p/0d14ae8f081c  #https://blog.csdn.net/wei389083222/article/details/78721074/ #https://blog.csdn.net/weixin_34368949/article/details/85991563 #驗證只能用post方法 #雖然流密鑰的格式像是get類型,但是必須使用POST獲取參數。 import pymysql from flask import Flask, request, Response app = Flask(__name__) #傳入url格式為: xx.xx.xx.xx:10078/user/auth?usr=xxx&passWord=xxx @app.route('/user/auth',methods=['POST']) def auth(): # 打開數據庫連接 db = pymysql.connect("ip地址","用戶名","密碼","數據庫名稱" ) # 使用 cursor() 方法創建一個游標對象 cursor cursor = db.cursor() username = request.form['username']#從url后獲取的數據 password = request.form['password'] print(username, '\t', password) # SQL查詢語句 sql = "select * from user where user_name = '%s' and user_password = '%s' " % (username, password) # 使用 execute() 方法執行 SQL 查詢 ret = cursor.execute(sql) if ret: return Response(response='success',status=200)#返回200狀態碼 else: return Response(status=500)#返回500狀態碼 #Flask.abort(404) return password if __name__ == '__main__': app.run(host='0.0.0.0',port=10078,debug=True)

運行auth.py之前,需要先安裝flask和pymysql

命令1:sudo pip install flask

命令2:sudo pip install pymysql

運行auth.py命令:python3 auth.py

 

通過OBS推流到nginx-rtmp流媒體服務器進行推流測試

推送不加user和pass的裸流:rtmp://ip地址:1935/hls/test01(推流失敗)

推送加user和pass的視頻流:rtmp://ip地址:1935/hls/test01?username=abc&password=123(推流成功)

 

2 配置播放權限

配置nginx-rtmp流媒體服務器的conf/nginx.conf文件;

 

rtmp {
     server {
         listen 1935; #監聽的端口 chunk_size 4000; application hls { #rtmp推流請求路徑 live on; hls on; hls_path /home/kaifang/nginx/hls; hls_fragment 5s; on_play http://localhost:10078/user/auth; # notify_method get; # on_publish http://localhost:81/on_publish.php;  } }

把auth.py放到任意目錄下面,運行:

auth.py代碼(使用固定用戶名密碼):

#!/usr/bin/env python
# -*- coding:utf-8 -*-

#https://www.jianshu.com/p/0d14ae8f081c

#https://blog.csdn.net/wei389083222/article/details/78721074/
#https://blog.csdn.net/weixin_34368949/article/details/85991563
#驗證只能用post方法
#雖然流密鑰的格式像是get類型,但是必須使用POST獲取參數。
from flask import Flask, request, Response
app = Flask(__name__)

#傳入url格式為: xx.xx.xx.xx:10078/user/auth?usr=xxx&passWord=xxx
@app.route('/user/auth',methods=['POST'])
def auth():
    usr = request.form['username']#從url后獲取的數據
    password = request.form['password']
    print(user,'\t',password)
    #此處可改為從數據庫獲取數據
    auth_user='abc'
    auth_passWord='123'
    if auth_user == user and auth_passWord ==password:
        return Response(response='success',status=200)#返回200狀態碼
    else:
        return Response(status=500)#返回500狀態碼
        #Flask.abort(404)
    return password


if __name__ == '__main__':
    app.run(host='0.0.0.0',port=10078,debug=True)

auth.py代碼(使用數據庫驗證):

#!/usr/bin/python3
# -*- coding:utf-8 -*-

#https://www.jianshu.com/p/0d14ae8f081c

#https://blog.csdn.net/wei389083222/article/details/78721074/
#https://blog.csdn.net/weixin_34368949/article/details/85991563
#驗證只能用post方法
#雖然流密鑰的格式像是get類型,但是必須使用POST獲取參數。
import pymysql
from flask import Flask, request, Response
app = Flask(__name__)

#傳入url格式為: xx.xx.xx.xx:10078/user/auth?usr=xxx&passWord=xxx
@app.route('/user/auth',methods=['POST'])
def auth():
    # 打開數據庫連接
    db = pymysql.connect("ip地址","用戶名","密碼","數據庫名稱" )

    # 使用 cursor() 方法創建一個游標對象 cursor
    cursor = db.cursor()
    
    username = request.form['username']#從url后獲取的數據
    password = request.form['password']
    print(username, '\t', password)

    # SQL查詢語句
    sql = "select * from user where user_name = '%s' and user_password = '%s' " % (username, password)

    # 使用 execute()  方法執行 SQL 查詢 
    ret = cursor.execute(sql)
    
    if ret:
        return Response(response='success',status=200)#返回200狀態碼
    else:
        return Response(status=500)#返回500狀態碼
        #Flask.abort(404)
    return password

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=10078,debug=True)

運行auth.py之前,需要先安裝flask和pymysql

命令1:sudo pip install flask

命令2:sudo pip install pymysql

運行auth.py命令:python3 auth.py

 

通過OBS推流到nginx-rtmp流媒體服務器進行播放測試

播放不加user和pass的裸流:rtmp://ip地址:1935/hls/test01(播放失敗)

播放加user和pass的視頻流:rtmp://ip地址:1935/hls/test01?username=abc&password=123(播放成功)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM