8 MSG_TYPE_SETCONF
= 0x0002
9 MSG_TYPE_GETCONF
= 0x0003
10 MSG_TYPE_SETEVENTS
= 0x0005
11 MSG_TYPE_AUTH
= 0x0007
13 EVENT_TYPE_BANDWIDTH
= 0x0004
14 EVENT_TYPE_WARN
= 0x0005
16 def parseHostAndPort(h
):
17 host
, port
= "localhost", 9051
24 print "Bad hostname %r"%h
34 def receive_message(s
):
37 length
,type = struct
.unpack("!HH",header
)
38 print "Got response length %d, type %d"%(length
,type)
41 print "Got response length %d, type %d, body %s"%(length
,type,body
)
42 return length
,type,body
44 def pack_message(type, body
=""):
46 reqheader
= struct
.pack("!HH", length
, type)
47 return "%s%s"%(reqheader
,body
)
50 s
.sendall(pack_message(MSG_TYPE_AUTH
))
51 length
,type,body
= receive_message(s
)
54 def get_option(s
,name
):
55 s
.sendall(pack_message(MSG_TYPE_GETCONF
,name
))
56 length
,type,body
= receive_message(s
)
59 def set_option(s
,msg
):
60 s
.sendall(pack_message(MSG_TYPE_SETCONF
,msg
))
61 length
,type,body
= receive_message(s
)
64 def get_event(s
,events
):
65 eventbody
= struct
.pack("!H", events
)
66 s
.sendall(pack_message(MSG_TYPE_SETEVENTS
,eventbody
))
67 length
,type,body
= receive_message(s
)
70 def listen_for_events(s
):
72 length
,type,body
= receive_message(s
)
75 def do_main_loop(host
,port
):
76 print "host is %s:%d"%(host
,port
)
77 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
78 s
.connect((host
,port
))
80 get_option(s
,"nickname")
81 get_option(s
,"DirFetchPostPeriod\n")
83 set_option(s
,"bandwidthburstbytes 100000")
84 # set_option(s,"runasdaemon 1")
85 # get_event(s,EVENT_TYPE_WARN)
86 get_event(s
,EVENT_TYPE_BANDWIDTH
)
92 if __name__
== '__main__':
93 if len(sys
.argv
) != 2:
94 print "Syntax: tor-control.py torhost:torport"
96 sh
,sp
= parseHostAndPort(sys
.argv
[1])