国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

詳解基于django實現的webssh簡單例子

2020-02-15 22:26:02
字體:
來源:轉載
供稿:網友

本文介紹了詳解基于django實現的webssh簡單例子,分享給大家,具體如下:

說明

新建一個 django 程序,本文為 chain。

以下僅為簡單例子,實際應用 可根據自己平臺情況 進行修改。

打開首頁后,需要輸入1,后臺去登錄主機,然后返回登錄結果。

正常項目 可以post 主機和登錄賬戶,進行權限判斷,然后去后臺讀取賬戶密碼,進行登錄。

djang后臺

需要安裝以下模塊

安裝后會有一個版本號報錯,不影響

channels==2.0.2channels-redis==2.1.0amqp==1.4.9anyjson==0.3.3asgi-redis==1.4.3asgiref==2.3.0async-timeout==2.0.0attrs==17.4.0cd /tmp/wget https://files.pythonhosted.org/packages/12/2a/e9e4fb2e6b2f7a75577e0614926819a472934b0b85f205ba5d5d2add54d0/Twisted-18.4.0.tar.bz2tar xf Twisted-18.4.0.tar.bz2cd Twisted-18.4.0python3 setup.py install

啟動redis

目錄

chain/    chain/       settings.py       asgi.py       consumers.py       routing.py  templates/      index.html

settings.py

# django-channels配置CHANNEL_LAYERS = {  "default": {    "BACKEND": "channels_redis.core.RedisChannelLayer",    "CONFIG": {      "hosts": [("127.0.0.1", 6379)],    },  },}# 配置ASGIASGI_APPLICATION = "chain.routing.application"

consumers.py

from asgiref.sync import async_to_syncfrom channels.generic.websocket import WebsocketConsumerimport paramikoimport threadingimport timefrom channels.layers import get_channel_layerchannel_layer = get_channel_layer()class MyThread(threading.Thread):  def __init__(self, id, chan):    threading.Thread.__init__(self)    self.chan = chan  def run(self):    while not self.chan.chan.exit_status_ready():      time.sleep(0.1)      try:        data = self.chan.chan.recv(1024)        async_to_sync(self.chan.channel_layer.group_send)(          self.chan.scope['user'].username,          {            "type": "user.message",            "text": bytes.decode(data)          },        )      except Exception as ex:        print(str(ex))    self.chan.sshclient.close()    return Falseclass EchoConsumer(WebsocketConsumer):  def connect(self):    # 創建channels group, 命名為:用戶名,并使用channel_layer寫入到redis    async_to_sync(self.channel_layer.group_add)(self.scope['user'].username, self.channel_name)    # 返回給receive方法處理    self.accept()  def receive(self, text_data):    if text_data == '1':      self.sshclient = paramiko.SSHClient()      self.sshclient.load_system_host_keys()      self.sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())      self.sshclient.connect('47.104.140.38', 22, 'root', '123456')      self.chan = self.sshclient.invoke_shell(term='xterm')      self.chan.settimeout(0)      t1 = MyThread(999, self)      t1.setDaemon(True)      t1.start()    else:      try:        self.chan.send(text_data)      except Exception as ex:        print(str(ex))  def user_message(self, event):    # 消費    self.send(text_data=event["text"])  def disconnect(self, close_code):    async_to_sync(self.channel_layer.group_discard)(self.scope['user'].username, self.channel_name)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奈曼旗| 通辽市| 施甸县| 高雄市| 苍梧县| 恩平市| 新津县| 杨浦区| 昌都县| 淮南市| 芦山县| 张掖市| 玉田县| 屏南县| 邹平县| 定结县| 肥东县| 南靖县| 农安县| 泸州市| 东宁县| 葵青区| 安顺市| 大余县| 沙雅县| 青田县| 襄樊市| 迁安市| 桃源县| 新巴尔虎左旗| 乌兰浩特市| 夏津县| 中江县| 石渠县| 罗城| 达拉特旗| 界首市| 屯门区| 大宁县| 白河县| 黑河市|