From 64ac582a54b00ab47c51d5d9a3e11fb59892ea88 Mon Sep 17 00:00:00 2001 From: Bondhan Novandy Date: Sun, 16 Sep 2018 21:17:24 +0700 Subject: [PATCH 1/2] porting to python3 --- ISO8583/ISO8583.py | 14 ++-- ISO8583/__init__.py | 4 +- README | 30 +------- examples/echoClient.py | 154 +++++++++++++++++++---------------------- examples/echoServer.py | 10 +-- examples/example1.py | 8 +-- 6 files changed, 95 insertions(+), 125 deletions(-) diff --git a/ISO8583/ISO8583.py b/ISO8583/ISO8583.py index a9e509f..58cf58b 100755 --- a/ISO8583/ISO8583.py +++ b/ISO8583/ISO8583.py @@ -406,9 +406,11 @@ def setBit(self, bit, value): self.BITMAP[0] = self.BITMAP[0] | self._TMP[2] # need to set bit 1 of first "bit" in bitmap if (bit % 8) == 0: - pos = (bit / 8) - 1 + # pos = (bit / 8) - 1 + pos = (bit // 8) - 1 else: - pos = (bit / 8) + # pos = (bit / 8) + pos = (bit // 8) # need to check if the value can be there .. AN , N ... etc ... and the size @@ -1160,6 +1162,8 @@ def getNetworkISO(self, bigEndian=True): netIso = "" asciiIso = self.getRawIso() + leng = len(asciiIso) + if bigEndian: netIso = struct.pack('!h', len(asciiIso)) if self.DEBUG == True: @@ -1169,9 +1173,9 @@ def getNetworkISO(self, bigEndian=True): if self.DEBUG == True: print('Pack Little-endian') - netIso += asciiIso + res = netIso + asciiIso.encode() - return netIso + return res ################################################################################################ @@ -1210,7 +1214,7 @@ def setNetworkISO(self, iso, bigEndian=True): if len(iso) < 24: raise InvalidIso8583('This is not a valid iso!!Invalid Size') - size = iso[0:2] + size = iso[0:2].encode() if bigEndian: size = struct.unpack('!h', size) if self.DEBUG == True: diff --git a/ISO8583/__init__.py b/ISO8583/__init__.py index 0523b9e..4bfd698 100755 --- a/ISO8583/__init__.py +++ b/ISO8583/__init__.py @@ -15,4 +15,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . -""" \ No newline at end of file +""" + +__all__ = ['ISO8583', 'ISOErrors'] \ No newline at end of file diff --git a/README b/README index e91db10..327a8d2 100755 --- a/README +++ b/README @@ -1,29 +1 @@ -(C) Copyright 2009 Igor V. Custodio - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -======================================================================== - -*) To install information, read INSTALL DOC - - -*) To read the documentation use pyDoc. - -*) To generate HTML documentation use: - 1) Go to doc dir - 2) Use the command: pydoc -w PATH_TO_ISO8583_UNCOMPRESSED_DIR - 3) That's it! - - -======================================================================== \ No newline at end of file +###Newbie porting (https://github.com/Seedstars/python-iso8583) to Python 3 \ No newline at end of file diff --git a/examples/echoClient.py b/examples/echoClient.py index ad19606..58b1fa4 100755 --- a/examples/echoClient.py +++ b/examples/echoClient.py @@ -17,103 +17,95 @@ """ - from ISO8583.ISO8583 import ISO8583 from ISO8583.ISOErrors import * import socket import sys import time - # Configure the client -serverIP = "192.168.0.103" -serverPort = 8583 +serverIP = "127.0.0.1" +serverPort = 7777 numberEcho = 5 -timeBetweenEcho = 5 # in seconds +timeBetweenEcho = 5 # in seconds bigEndian = True -#bigEndian = False +# bigEndian = False s = None for res in socket.getaddrinfo(serverIP, serverPort, socket.AF_UNSPEC, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: - s = socket.socket(af, socktype, proto) - except socket.error, msg: - s = None - continue + s = socket.socket(af, socktype, proto) + except socket.error as msg: + s = None + continue try: - s.connect(sa) - except socket.error, msg: - s.close() - s = None - continue + s.connect(sa) + except socket.error as msg: + s.close() + s = None + continue break if s is None: - print ('Could not connect :(') + print('Could not connect :(') sys.exit(1) - - - -for req in range(0,numberEcho): - iso = ISO8583() - iso.setMTI('0800') - iso.setBit(3,'300000') - iso.setBit(24,'045') - iso.setBit(41,'11111111') - iso.setBit(42,'222222222222222') - iso.setBit(63,'This is a Test Message') - if bigEndian: - try: - message = iso.getNetworkISO() - s.send(message) - print ('Sending ... %s' % message) - ans = s.recv(2048) - print ("\nInput ASCII |%s|" % ans) - isoAns = ISO8583() - isoAns.setNetworkISO(ans) - v1 = isoAns.getBitsAndValues() - for v in v1: - print ('Bit %s of type %s with value = %s' % (v['bit'],v['type'],v['value'])) - - if isoAns.getMTI() == '0810': - print ("\tThat's great !!! The server understand my message !!!") - else: - print ("The server dosen't understand my message!") - - except InvalidIso8583, ii: - print ii - break - - - time.sleep(timeBetweenEcho) - - else: - try: - message = iso.getNetworkISO(False) - s.send(message) - print ('Sending ... %s' % message) - ans = s.recv(2048) - print ("\nInput ASCII |%s|" % ans) - isoAns = ISO8583() - isoAns.setNetworkISO(ans,False) - v1 = isoAns.getBitsAndValues() - for v in v1: - print ('Bit %s of type %s with value = %s' % (v['bit'],v['type'],v['value'])) - - if isoAns.getMTI() == '0810': - print ("\tThat's great !!! The server understand my message !!!") - else: - print ("The server dosen't understand my message!") - - except InvalidIso8583, ii: - print ii - break - - time.sleep(timeBetweenEcho) - - - -print ('Closing...') -s.close() - \ No newline at end of file + +for req in range(0, numberEcho): + iso = ISO8583() + iso.setMTI('0800') + iso.setBit(3, '300000') + iso.setBit(24, '045') + iso.setBit(41, '11111111') + iso.setBit(42, '222222222222222') + iso.setBit(63, 'This is a Test Message') + if bigEndian: + try: + message = iso.getNetworkISO() + s.send(message) + print('Sending ... %s' % message) + ans = s.recv(2048).decode() + print("\nInput ASCII |%s|" % ans) + isoAns = ISO8583() + isoAns.setNetworkISO(ans) + v1 = isoAns.getBitsAndValues() + for v in v1: + print('Bit %s of type %s with value = %s' % (v['bit'], v['type'], v['value'])) + + if isoAns.getMTI() == '0810': + print("\tThat's great !!! The server understand my message !!!") + else: + print("The server dosen't understand my message!") + + except InvalidIso8583 as ii: + print(ii) + break + + time.sleep(timeBetweenEcho) + + else: + try: + message = iso.getNetworkISO(False) + s.send(message) + print('Sending ... %s' % message) + ans = s.recv(2048) + print("\nInput ASCII |%s|" % ans) + isoAns = ISO8583() + isoAns.setNetworkISO(ans, False) + v1 = isoAns.getBitsAndValues() + for v in v1: + print('Bit %s of type %s with value = %s' % (v['bit'], v['type'], v['value'])) + + if isoAns.getMTI() == '0810': + print("\tThat's great !!! The server understand my message !!!") + else: + print("The server dosen't understand my message!") + + except InvalidIso8583 as ii: + print(ii) + break + + time.sleep(timeBetweenEcho) + +print('Closing...') +s.close() diff --git a/examples/echoServer.py b/examples/echoServer.py index 6d4a63e..e4db6d2 100755 --- a/examples/echoServer.py +++ b/examples/echoServer.py @@ -23,8 +23,8 @@ from socket import * # Configure the server -serverIP = "192.168.0.103" -serverPort = 8583 +serverIP = "127.0.0.1" +serverPort = 7777 maxConn = 5 bigEndian = True #bigEndian = False @@ -44,7 +44,7 @@ connection, address = s.accept() while 1: # receive message - isoStr = connection.recv(2048) + isoStr = connection.recv(2048).decode() if isoStr: print ("\nInput ASCII |%s|" % isoStr) pack = ISO8583() @@ -66,8 +66,8 @@ break - except InvalidIso8583, ii: - print ii + except InvalidIso8583 as ii: + print(ii) break except: print ('Something happened!!!!') diff --git a/examples/example1.py b/examples/example1.py index 8edf661..8690846 100755 --- a/examples/example1.py +++ b/examples/example1.py @@ -87,8 +87,8 @@ try: print ('Bit 27 is there? %s' % p.getBit(27)) -except BitNotSet, bns: - print bns +except BitNotSet as bns: + print(bns) #More exceptions... @@ -105,9 +105,9 @@ iso.setBit(17,17) iso.setBit(49,9861) # this bit is wrong ... iso.setBit(99,99) -except ValueToLarge, e: +except ValueToLarge as e: print ('Value too large :( %s' % e) -except InvalidMTI, i: +except InvalidMTI as i: print ('This MTI is wrong :( %s' % i) From 19e74c159fb567d961cff38f7e3ca4c1d9959a75 Mon Sep 17 00:00:00 2001 From: Bondhan Novandy Date: Sun, 16 Sep 2018 21:18:14 +0700 Subject: [PATCH 2/2] Update README --- README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README b/README index 327a8d2..ca6ca2b 100755 --- a/README +++ b/README @@ -1 +1 @@ -###Newbie porting (https://github.com/Seedstars/python-iso8583) to Python 3 \ No newline at end of file +Newbie porting (https://github.com/Seedstars/python-iso8583) to Python 3