monitor_client.py :  » Web-Frameworks » Zope » Zope-2.6.0 » ZServer » medusa » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Zope 
Zope » Zope 2.6.0 » ZServer » medusa » monitor_client.py
# -*- Mode: Python; tab-width: 4 -*-

# monitor client, unix version.

import asyncore
import asynchat
import regsub
import socket
import string
import sys
import os

import md5
import time

class stdin_channel (asyncore.file_dispatcher):
    def handle_read (self):
        data = self.recv(512)
        if not data:
            print '\nclosed.'
            self.sock_channel.close()
            try:
                self.close()
            except:
                pass
                
        data = regsub.gsub ('\n', '\r\n', data)
        self.sock_channel.push (data)
        
    def writable (self):
        return 0
        
    def log (self, *ignore):
        pass
        
class monitor_client (asynchat.async_chat):
    def __init__ (self, password, addr=('',8023), socket_type=socket.AF_INET):
        asynchat.async_chat.__init__ (self)
        self.create_socket (socket_type, socket.SOCK_STREAM)
        self.terminator = '\r\n'
        self.connect (addr)
        self.sent_auth = 0
        self.timestamp = ''
        self.password = password
        
    def collect_incoming_data (self, data):
        if not self.sent_auth:
            self.timestamp = self.timestamp + data
        else:
            sys.stdout.write (data)
            sys.stdout.flush()
            
    def found_terminator (self):
        if not self.sent_auth:
            self.push (hex_digest (self.timestamp + self.password) + '\r\n')
            self.sent_auth = 1
        else:
            print
            
    def handle_close (self):
            # close all the channels, which will make the standard main
            # loop exit.
        map (lambda x: x.close(), asyncore.socket_map.values())
        
    def log (self, *ignore):
        pass
        
class encrypted_monitor_client (monitor_client):
    "Wrap push() and recv() with a stream cipher"
    
    def init_cipher (self, cipher, key):
        self.outgoing = cipher.new (key)
        self.incoming = cipher.new (key)
        
    def push (self, data):
            # push the encrypted data instead
        return monitor_client.push (self, self.outgoing.encrypt (data))
        
    def recv (self, block_size):
        data = monitor_client.recv (self, block_size)
        if data:
            return self.incoming.decrypt (data)
        else:
            return data
            
def hex_digest (s):
    m = md5.md5()
    m.update (s)
    return string.join (
            map (lambda x: hex (ord (x))[2:], map (None, m.digest())),
            '',
            )
    
if __name__ == '__main__':
    if len(sys.argv) == 1:
        print 'Usage: %s host port' % sys.argv[0]
        sys.exit(0)
        
    if ('-e' in sys.argv):
        encrypt = 1
        sys.argv.remove ('-e')
    else:
        encrypt = 0
        
    sys.stderr.write ('Enter Password: ')
    sys.stderr.flush()
    import os
    try:
        os.system ('stty -echo')
        p = raw_input()
        print
    finally:
        os.system ('stty echo')
    stdin = stdin_channel (0)
    if len(sys.argv) > 1:
        if encrypt:
            client = encrypted_monitor_client (p, (sys.argv[1], string.atoi (sys.argv[2])))
            import sapphire
            client.init_cipher (sapphire, p)
        else:
            client = monitor_client (p, (sys.argv[1], string.atoi (sys.argv[2])))
    else:
            # default to local host, 'standard' port
        client = monitor_client (p)
    stdin.sock_channel = client
    asyncore.loop()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.