1.下载noVNC项目,把他放到你的django项目的static目录下,我们这里假设是/djangproject/static/noVNC
2.在noVNC目录下创建一个目录vnc_tokens,并在vnc_tokens下传创建hosts.conf文件,我们将把要通过django访问的vnc主机信息写在hosts.conf文件,格式如下:
#token名: VNC的IP地址:VNC端口 web_server: 1.1.1.1:5901 dns_server: 1.1.2.2:5902
3.安装websockify
pip3 install websockify
4.在django项目的wsgi.py里添加以下代码启动代理,我们这里代理端口设置为6080
from multiprocessing import Process
import socket
def worker():
dir_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'static')
websockify_path = '/usr/local/bin/websockify'
web_path = os.path.join(dir_path, 'noVNC')
target_path = os.path.join(dir_path, 'noVNC', 'vnc_tokens/hosts.conf')
cmd = “python3 {} --web={} --target-config={} {}”.format(websockify_path, web_path, target_path, 6080)
os.system(cmd)
def is_open():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(('127.0.0.1', 6080))
s.shutdown(2)
return True
except:
return False
def start_websockify():
'''
如果已经启动则不启动
'''
if not is_open():
t = Process(target=worker, args=())
t.start()
start_websockify()
5.创建一个单独的django app或者在已有的app,假如我们的app就叫app, 在view里添加:
from django.shortcuts import render
def proxy_vnc(request, token):
#这里的token就是我们第二部定义的token名
host = 'server lan ip'
port = 6080
password = 'password'
return render(request, 'app/vnc_lite.html', {
"host":host,
"port":port,
"token":token,
"view_only": 'false',
"path":'?' + token,
"password":password,
})
urls.py里添加
path('app/<str:token>', views.proxy_vnc, name=proxy_vnc),
复制/djangproject/static/noVNC/vnc_lite.html到你app的template目录下。
启动uwsgi,现在就就可以利用下命的url访问我们的vnc了
http://host/app/web_server
http://host/app/dns_server