File: paraxml.py

package info (click to toggle)
bitpim 1.0.7%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 31,384 kB
  • sloc: python: 267,746; cpp: 2,076; perl: 600; ansic: 409; sh: 226; makefile: 152; sed: 1
file content (111 lines) | stat: -rw-r--r-- 3,529 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Trying to do xml-rpc using ssh (via paramiko library)

import  paramiko_bp as paramiko # our version is slightly modified
import socket
import sys
import threading, thread

print "Main thread is",thread.get_ident()


bind_address=("", 12098)
connect_address=("localhost", 12098)

class ServerChannel(paramiko.Channel):
    pass
    

class ServerTransport(paramiko.Transport):
    okauth=( ('rogerb', 'password'), ('example', 'nonsense') )
    def check_channel_request(self, kind, chanid):
        print "check channel request in thread",thread.get_ident()
        if kind == 'bitfling':
            return ServerChannel(chanid)
        return self.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED

    def check_auth_password(self, username, password):
        print "check auth request in thread",thread.get_ident()
        print "checking creds",`username`, `password`
        if (username,password) in self.okauth:
            return self.AUTH_SUCCESSFUL
        return self.AUTH_FAILED

    def get_allowed_auths(self, username):
        return 'password'

def server():
    # setup logging
    import logging
    l = logging.getLogger("paramiko")
    l.setLevel(logging.DEBUG)
    if len(l.handlers) == 0:
        f = open('demo_server.log', 'wt')
        lh = logging.StreamHandler(f)
        lh.setFormatter(logging.Formatter('%(levelname)-.3s [%(asctime)s] %(name)s: %(message)s', '%Y%m%d:%H%M%S'))
        l.addHandler(lh)

    host_key = paramiko.DSSKey()
    host_key.read_private_key_file('pmkey')
    
    sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(bind_address)
    sock.listen(5)

    while True:
        conn, addr=sock.accept()
        print "accept from",addr
        transport=ServerTransport(conn)
        transport.add_server_key(host_key)
        print "starting server"
        transport.start_server()
        print "accepting channel"
        chan=transport.accept()
        print "got a channel"
        while True:
            try:
                c=chan.recv(1)
                print "received",c
            except EOFError:
                c=None
            if c is None or len(c)==0:
                print "received end of channel"
                transport.close()
                break
            chan.sendall(chr(ord(c)+1))

def client():
    # setup logging
        # setup logging
    import logging
    l = logging.getLogger("paramiko")
    l.setLevel(logging.DEBUG)
    if len(l.handlers) == 0:
        f = open('demo_client.log', 'wt')
        lh = logging.StreamHandler(f)
        lh.setFormatter(logging.Formatter('%(levelname)-.3s [%(asctime)s] %(name)s: %(message)s', '%Y%m%d:%H%M%S'))
        l.addHandler(lh)
    sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(connect_address)
    transport=paramiko.Transport(sock)
    transport.setDaemon(True)
    event=threading.Event()
    transport.start_client(event)
    event.wait(15)
    print "active =",transport.is_active()
    keytype, hostkey = transport.get_remote_server_key()
    print "host key is",keytype,paramiko.util.hexify(hostkey)
    event=threading.Event()
    transport.auth_password('rogerb', 'password', event)
    event.wait(10)
    chan=transport.open_channel("bitfling")
    import random
    for i in range(10):
        chan.sendall(random.choice( ("one", "two", "three") )+" ")
        print chan.recv(4)

if __name__=='__main__':
    if sys.argv[1]=='server':
        server()
    else:
        client()