首页 > 编程 > Python > 正文

python制作websocket服务器实例分享

2020-02-23 04:07:34
字体:
来源:转载
供稿:网友

一、开始的话

  使用python简单的实现websocket服务器,可以在浏览器上实时显示远程服务器的日志信息。

  之前做了一个web版的发布系统,但没实现在线看日志,每次发布版本后,都需要登录到服务器上查看日志,非常麻烦,为了偷懒,能在页面点几下按钮完成工作,这几天查找了这方面的资料,实现了这个功能,瞬间觉的看日志什么的,太方便了,以后也可以给开发们查日志,再也不用麻烦运维了,废话少说,先看效果吧。

二、代码

  需求:在web上弹出iframe层来实时显示远程服务器的日志,点击stop按钮,停止日志输出,以便查看相关日志,点start按钮,继续输出日志,点close按钮,关闭iframe层。

  在实现这功能前,google了一些资料,发现很多只能在web上显示本地的日志,不能看远程服务器的日志,能看远程日志的是引用了其他框架(例如bottle,tornado)来实现的,而且所有这些都是要重写thread的run方法来实现的,由于本人技术太菜,不知道怎么改成自己需要的样子,而且我是用django这个web框架的,不想引入其他框架,搞的太复杂,所以用python简单的实现websocket服务器。recv_data方法和send_data是直接引用别人的代码。由于技术问题,代码有点粗糙,不过能实现功能就行,先将就着用吧。

执行下面命令启动django和websocketserver

nohup python manage.py runserver 10.1.12.110 &nohup python websocketserver.py &

  启动websocket后,接收到请求,起一个线程和客户端握手,然后根据客户端发送的ip和type,去数据库查找对应的日志路径,用paramiko模块ssh登录到远程服务器上tail查看日志,再推送给浏览器,服务端完整代码如下:

# coding:utf-8import osimport structimport base64import hashlibimport socketimport threadingimport paramikodef get_ssh(ip, user, pwd):  try:    ssh = paramiko.SSHClient()    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())    ssh.connect(ip, 22, user, pwd, timeout=15)    return ssh  except Exception, e:    print e    return "False"def recv_data(conn):  # 服务器解析浏览器发送的信息  try:    all_data = conn.recv(1024)    if not len(all_data):      return False  except:    pass  else:    code_len = ord(all_data[1]) & 127    if code_len == 126:      masks = all_data[4:8]      data = all_data[8:]    elif code_len == 127:      masks = all_data[10:14]      data = all_data[14:]    else:      masks = all_data[2:6]      data = all_data[6:]    raw_str = ""    i = 0    for d in data:      raw_str += chr(ord(d) ^ ord(masks[i % 4]))      i += 1    return raw_strdef send_data(conn, data):  # 服务器处理发送给浏览器的信息  if data:    data = str(data)  else:    return False  token = "/x81"  length = len(data)  if length < 126:    token += struct.pack("B", length)  # struct为Python中处理二进制数的模块,二进制流为C,或网络流的形式。  elif length <= 0xFFFF:    token += struct.pack("!BH", 126, length)  else:    token += struct.pack("!BQ", 127, length)  data = '%s%s' % (token, data)  conn.send(data)  return Truedef handshake(conn, address, thread_name):  headers = {}  shake = conn.recv(1024)  if not len(shake):    return False  print ('%s : Socket start handshaken with %s:%s' % (thread_name, address[0], address[1]))  header, data = shake.split('/r/n/r/n', 1)  for line in header.split('/r/n')[1:]:    key, value = line.split(': ', 1)    headers[key] = value  if 'Sec-WebSocket-Key' not in headers:    print ('%s : This socket is not websocket, client close.' % thread_name)    conn.close()    return False  MAGIC_STRING = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'  HANDSHAKE_STRING = "HTTP/1.1 101 Switching Protocols/r/n" /            "Upgrade:websocket/r/n" /            "Connection: Upgrade/r/n" /            "Sec-WebSocket-Accept: {1}/r/n" /            "WebSocket-Origin: {2}/r/n" /            "WebSocket-Location: ws://{3}//r/n/r/n"  sec_key = headers['Sec-WebSocket-Key']  res_key = base64.b64encode(hashlib.sha1(sec_key + MAGIC_STRING).digest())  str_handshake = HANDSHAKE_STRING.replace('{1}', res_key).replace('{2}', headers['Origin']).replace('{3}', headers['Host'])  conn.send(str_handshake)  print ('%s : Socket handshaken with %s:%s success' % (thread_name, address[0], address[1]))  print 'Start transmitting data...'  print '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'  return Truedef dojob(conn, address, thread_name):  handshake(conn, address, thread_name)   # 握手  conn.setblocking(0)            # 设置socket为非阻塞  ssh = get_ssh('192.168.1.1', 'root', '123456')  # 连接远程服务器  ssh_t = ssh.get_transport()  chan = ssh_t.open_session()  chan.setblocking(0)  # 设置非阻塞  chan.exec_command('tail -f /var/log/messages')  while True:    clientdata = recv_data(conn)    if clientdata is not None and 'quit' in clientdata:  # 但浏览器点击stop按钮或close按钮时,断开连接      print ('%s : Socket close with %s:%s' % (thread_name, address[0], address[1]))      send_data(conn, 'close connect')      conn.close()      break    while True:      while chan.recv_ready():        clientdata1 = recv_data(conn)        if clientdata1 is not None and 'quit' in clientdata1:          print ('%s : Socket close with %s:%s' % (thread_name, address[0], address[1]))          send_data(conn, 'close connect')          conn.close()          break        log_msg = chan.recv(10000).strip()  # 接收日志信息        print log_msg        send_data(conn, log_msg)      if chan.exit_status_ready():        break      clientdata2 = recv_data(conn)      if clientdata2 is not None and 'quit' in clientdata2:        print ('%s : Socket close with %s:%s' % (thread_name, address[0], address[1]))        send_data(conn, 'close connect')        conn.close()        break    breakdef ws_service():  index = 1  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  sock.bind(("127.0.0.1", 12345))  sock.listen(100)  print ('/r/n/r/nWebsocket server start, wait for connect!')  print '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'  while True:    connection, address = sock.accept()    thread_name = 'thread_%s' % index    print ('%s : Connection from %s:%s' % (thread_name, address[0], address[1]))    t = threading.Thread(target=dojob, args=(connection, address, thread_name))    t.start()    index += 1ws_service()            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表