`
xiaolin0199
  • 浏览: 566071 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

NoVNC的使用之二: 将Novnc整合进django项目

 
阅读更多

 

上一篇已经讲了如何让NoVNC代理跑起来 , 这章我们来看看如何将它集成到我们的django项目中来

NoVNC的使用之一: 让我们把NoVNC代理跑起来

 

1. 设置静态文件 (包括NoVNC需要用到的 css , js , image , template 文件)

写道

这里我们假设 /media/ 目录是你的静态文件目录 , 这个目录下有 css , js , images等目录....

1. 我们创建一个 vnc的目录 , mkdir vnc

2. cp -R ~/work/noVNC/images -> media/vnc
cp -R ~/work/noVNC/include -> media/vnc

3. cp ~/work/noVNC/vnc_auto.html -> templates/vnc_auto.html

let us edit this :
// host = WebUtil.getQueryVar('host', window.location.hostname);
host = '{{host}}'
// port = WebUtil.getQueryVar('port', window.location.port);
port = {{port}}

// path = WebUtil.getQueryVar('path', 'websockify');
path = WebUtil.getQueryVar('path', 'websockify/?token={{token}}');

// 'view_only': WebUtil.getQueryVar('view_only', false),
'view_only': WebUtil.getQueryVar('view_only', {{view_only}}),

 

2.  将noVNC代理程序移植到项目中..

 

写道
这里我将VNC的关键代码放到项目的 utils目录下


我们只需要原代码中的 noVNC/utils 即可


cp -R ~/work/noVNC/utils  ../../utils/vnc

 

3.  添加配置文件夹

写道

紧接着上面 , 在 ../../utils/vnc 下 mkdir vnc_tokens 目录

cd vnc_tokens

vim vnc-1.ini and edit is

abcdefg: 10.10.10.202:5900


×× 如果有多个被控端,就顺着往下写即可,不用一个被控端一个 ini 文件

 

4.  如果在项目中启动 novnc 代理服务

 

写道

1. 线程 (threading) , 不行, 这个服务只能在主线程中跑

2. 子进程 (subprocess), 不行, 主进程会被阻塞

3. 多进程 (multiprocessing) , OK

   

#coding=utf-8
import os
from multiprocessing import Process

from django.conf import settings

def worker():
    '''
        多进程启动VNC服务
    '''
    
    dir_path = os.path.dirname(__file__)
    
    websockify_path = os.path.join(dir_path, 'vnc', 'utils', 'websockify')
    web_path = os.path.join(dir_path, 'vnc')
    target_path = os.path.join(dir_path, 'vnc', 'vnc_tokens')
    
    cmd = u'python %s --web=%s --target-config=%s %s' %(websockify_path, web_path, target_path, settings.VNC_PROXY_PORT)
    
    os.system(cmd)  
    
def start_websockify():
    '''
        启动vnc代理服务
        ./utils/websockify --web=. --target-config=vnc_tokens 6080  
        {'target_cfg': '/home/xiaofei/work/noVNC/vnc_tokens', 'listen_port': 6080}
    '''

    print u'start vnc proxy..'
    
    t = Process(target=worker,args=())
    t.start()

    print u'vnc proxy started..'

 

 

5.  写一个URL响应函数, 就可以用类似下面的方式 进行 访问 :

def run_vnc(request):
    '''
        调用VNC代理进行远程控制
    '''
    template = loader.get_template('vnc_auto.html')
                
    token = request.GET.get('token', '') # 用来判断不同的访问机器
    view_only = request.GET.get('view_only', 'false') # false可以控制 or true只能查看

    # 代理服务器 IP及端口 , 这个一般都用校服务器局域网IP (127.0.0.1, 6080是默认端口)
    host = '127.0.0.1'
    port = settings.VNC_PROXY_PORT
        
    context = Context({'token':token, 'host':host, 'port': port, 'view_only':view_only})
    

    return HttpResponse(template.render(context)) 

 

6.  http://localhost:8000/system/maintenance/run-vnc/?token=abcdefg  即可控制 10.10.10.202机器 (前提是202开启了  vcn server)

 



 

 

  • 大小: 111.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics