]> git.evergreen-ils.org Git - working/SIPServer.git/blob - acstest.py
LP1528301: (follow-up) tweak whitespace
[working/SIPServer.git] / acstest.py
1 #!/usr/bin/python
2 # Copyright (C) 2006-2008  Georgia Public Library Service
3
4 # Author: David J. Fiander
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 import operator
20 import socket
21 from time import strftime;
22
23 def SipSocket(host='localhost', port=5300):
24     so = socket.socket()
25     so.connect((host, port))
26     return so
27
28 def login(so, uname='scclient', passwd='clientpwd', locn='The basement',
29           seqno=0):
30     port = so.getpeername()[1]
31     if port == 5300:
32         resp = send(so, '9300CN%s|CO%s|CP%s|' % (uname, passwd, locn), seqno)
33         print "Received", repr(resp)
34         print "Verified: ", verify(resp)
35     else:
36         raise "Logging in is only support for the raw transport on port 5300"
37
38 def send(so, msg, seqno=0):
39     if seqno:
40         msg += 'AY' + str(seqno)[0] + 'AZ'
41         msg += ('%04X' % calculate_cksum(msg))
42     msg += '\r'
43     print 'Sending', repr(msg)
44     so.send(msg)
45     resp = so.recv(1000)
46     return resp, verify(resp)
47
48 def calculate_cksum(msg):
49     return (-reduce(operator.add, map(ord, msg)) & 0xFFFF)
50
51 def sipdate():
52     return(strftime("%Y%m%d    %H%M%S"))
53
54 def verify(msg):
55     if msg[-1] == '\r': msg = msg[:-2]
56     if msg[-6:-4] == 'AZ':
57         cksum = calculate_cksum(msg[:-4])
58         return (msg[-4:] == ('%04X' % cksum))
59     # If there's no checksum, then the message is ok
60     return True