SHA 512 crypt output written with Python code is different from mkpasswd - python

Running mkpasswd -m sha-512 -S salt1234 password results in the following:
$6$salt1234$Zr07alHmuONZlfKILiGKKULQZaBG6Qmf5smHCNH35KnciTapZ7dItwaCv5SKZ1xH9ydG59SCgkdtsTqVWGhk81
I have this snippet of Python code that I thought would output the same, but isn't:
import hashlib, base64
print(base64.b64encode(hashlib.sha512('password' + 'salt1234').digest()))
It instead results in:
nOkBUt6l7zlKAfjtk1EfB0TmckXfDiA4FPLcpywOLORZ1PWQK4+PZVEiT4+9rFjqR3xnaruZBiRjDGcDpxxTig==
Not sure what I am doing wrong.
Another question I have is, how do I tell sha512 function to do custom rounds. It seems to take only 1 argument.

mkpasswd is a front-end to the crypt() function. I don't think it is a straight-forward SHA512 hash here.
A little research points to the specification for SHA256-crypt and SHA512-crypt, which shows the hash is applied a default 5000 times. You can specify a different number of rounds using the -R switch to mkpasswd; -R 5000 indeed gives you the same output:
$ mkpasswd -m sha-512 -S salt1234 -R 5000 password
$6$rounds=5000$salt1234$Zr07alHmuONZlfKILiGKKULQZaBG6Qmf5smHCNH35KnciTapZ7dItwaCv5SKZ1xH9ydG59SCgkdtsTqVWGhk81
The minimum number of rounds offered by the command-line tool is 1000:
$ mkpasswd -m sha-512 -S salt1234 -R 999 password
$6$rounds=1000$salt1234$SVDFHbJXYrzjGi2fA1k3ws01/D9q0ZTAh1KfRF5.ehgjVBqfHUaKqfynXefJ4DxIWxkMAITYq9mmcBl938YQ//
$ mkpasswd -m sha-512 -S salt1234 -R 1 password
$6$rounds=1000$salt1234$SVDFHbJXYrzjGi2fA1k3ws01/D9q0ZTAh1KfRF5.ehgjVBqfHUaKqfynXefJ4DxIWxkMAITYq9mmcBl938YQ//
The algorithm is a bit more involved, requiring you to create several digests. You could instead access the C crypt() function through the crypt.crypt() function, and drive it the same way the mkpasswd commandline does.
It depends on your platform if the SHA512-crypt method is available; the Python 3 version of the crypt module offers a crypt.methods list that tells you what methods your platform supports. Since this use the exact same library mkpasswd uses, your OS obviously does support SHA512-crypt and Python will have access too.
You need to prefix the salt with '$6$ to specify the different method. You can specify the number of rounds by adding a 'rounds=<N>$' string between the '$6$' string and your salt:
import crypt
import os
import string
try: # 3.6 or above
from secrets import choice as randchoice
except ImportError:
from random import SystemRandom
randchoice = SystemRandom().choice
def sha512_crypt(password, salt=None, rounds=None):
if salt is None:
salt = ''.join([randchoice(string.ascii_letters + string.digits)
for _ in range(8)])
prefix = '$6$'
if rounds is not None:
rounds = max(1000, min(999999999, rounds or 5000))
prefix += 'rounds={0}$'.format(rounds)
return crypt.crypt(password, prefix + salt)
This then produces the same output as the mkpasswd command line:
>>> sha512_crypt('password', 'salt1234')
'$6$salt1234$Zr07alHmuONZlfKILiGKKULQZaBG6Qmf5smHCNH35KnciTapZ7dItwaCv5SKZ1xH9ydG59SCgkdtsTqVWGhk81'
>>> sha512_crypt('password', 'salt1234', rounds=1000)
'$6$rounds=1000$salt1234$SVDFHbJXYrzjGi2fA1k3ws01/D9q0ZTAh1KfRF5.ehgjVBqfHUaKqfynXefJ4DxIWxkMAITYq9mmcBl938YQ//'

You need to use crypt.crypt:
>>> import crypt
>>> crypt.crypt('password', '$6$' + 'salt1234')
'$6$salt1234$Zr07alHmuONZlfKILiGKKULQZaBG6Qmf5smHCNH35KnciTapZ7dItwaCv5SKZ1xH9ydG59SCgkdtsTqVWGhk81'

Here is a pure python3 implementation of the sha512_crypt function based on the specification. This is for illustration only, always use crypt.crypt instead!
import hashlib, base64
SHUFFLE_SHA512_INDICES = [
42, 21, 0, 1, 43, 22, 23, 2, 44, 45, 24, 3, 4, 46, 25,
26, 5, 47, 48, 27, 6, 7, 49, 28, 29, 8, 50, 51, 30, 9,
10, 52, 31, 32, 11, 53, 54, 33, 12, 13, 55, 34, 35, 14, 56,
57, 36, 15, 16, 58, 37, 38, 17, 59, 60, 39, 18, 19, 61, 40,
41, 20, 62, 63
]
def shuffle_sha512(data):
return bytes(data[i] for i in SHUFFLE_SHA512_INDICES)
def extend_by_repeat(data, length):
return (data * (length // len(data) + 1))[:length]
CUSTOM_ALPHABET = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
''' Base64 encode based on SECTION 22.e)
'''
def custom_b64encode(data, alphabet = CUSTOM_ALPHABET):
buffer,count,result = 0,0,[]
for byte in data:
buffer |= byte << count
count += 8
while count >= 6:
result.append(buffer & 0x3f)
buffer >>= 6
count -= 6
if count > 0:
result.append(buffer)
return ''.join(alphabet[idx] for idx in result)
''' From http://www.akkadia.org/drepper/SHA-crypt.txt
'''
def sha512_crypt(password, salt, rounds_in = None):
rounds,rounds_defined = 5000, False
if rounds_in is not None:
rounds,rounds_defined = rounds_in, True
assert 1000 <= rounds <= 999999999
hash = hashlib.sha512
salt_prefix = '$6$'
password = password.encode('utf8')
salt = salt.encode('ascii')[:16]
A = hash() # SECTION 1.
A.update(password) # SECTION 2.
A.update(salt) # SECTION 3.
B = hash() # SECTION 4.
B.update(password) # SECTION 5.
B.update(salt) # SECTION 6.
B.update(password) # SECTION 7.
digestB = B.digest(); # SECTION 8.
A.update(extend_by_repeat(digestB, len(password))) # SECTION 9., 10.
# SECTION 11.
i = len(password)
while i > 0:
if i & 1:
A.update(digestB) # SECTION 11.a)
else:
A.update(password) # SECTION 11.b)
i = i >> 1
digestA = A.digest() # SECTION 12.
DP = hash() # SECTION 13.
# SECTION 14.
for _ in range(len(password)):
DP.update(password)
digestDP = DP.digest() # SECTION 15.
P = extend_by_repeat(digestDP, len(password)) # SECTION 16.a), 16.b)
DS = hash() # SECTION 17.
# SECTION 18.
for _ in range(16 + digestA[0]):
DS.update(salt)
digestDS = DS.digest() # SECTION 19.
S = extend_by_repeat(digestDS, len(salt)) # SECTION 20.a), 20.b)
# SECTION 21.
digest_iteration_AC = digestA
for i in range(rounds):
C = hash() # SECTION 21.a)
if i % 2:
C.update(P) # SECTION 21.b)
else:
C.update(digest_iteration_AC) # SECTION 21.c)
if i % 3:
C.update(S) # SECTION 21.d)
if i % 7:
C.update(P) # SECTION 21.e)
if i % 2:
C.update(digest_iteration_AC) # SECTION 21.f)
else:
C.update(P) # SECTION 21.g)
digest_iteration_AC = C.digest() # SECTION 21.h)
shuffled_digest = shuffle_sha512(digest_iteration_AC)
prefix = salt_prefix # SECTION 22.a)
# SECTION 22.b)
if rounds_defined:
prefix += 'rounds={0}$'.format(rounds_in)
return (prefix
+ salt.decode('ascii') # SECTION 22.c)
+ '$' # SECTION 22.d)
+ custom_b64encode(shuffled_digest) # SECTION 22.e)
)
actual = sha512_crypt('password', 'salt1234')
expected = '$6$salt1234$Zr07alHmuONZlfKILiGKKULQZaBG6Qmf5smHCNH35KnciTapZ7dItwaCv5SKZ1xH9ydG59SCgkdtsTqVWGhk81'
print(actual)
print(expected)
assert actual == expected

Related

Attempting to Traverse a search API Collecting results in Python 3.9

I am attempting to yield all results from a search api.
There are 3 key cases:
Case 1: There are 0 results yielded
Case 2: There are 1-9 results yielded
Case 3: There are 10 results yielded
In both cases 1 and 2, we can conclude that there are no results deeper. i.e if we searched for "iiiiia" and it yields 0 then that means there are no results for "iiiiia" and no results deeper than "iiiiia". If "iiiiia" yields 1-9 results, then we can concluded "iiiiia" yields 109 results and no results deeper.
In case 3, if we search for "iiiiia" and it yields 10, we can conclude that "iiiiia" has 10 results, and there may or may not be results deeper as 10 means 10+ results possible. In this case we would have to move one layer down the chain and traverse through those results. i.e "iiiiia0"-"iiiiiaz" and if any of those yield 10 we would also have to go one layer deeper.
an example of how this may look:
a
a0
a00
a000
a0000
a0001
...
a000z
a001
a002
a003
a0030
...
a0034
a00340
...
a0034z
a0035...
Here is my attempted code:
import json
from time import sleep
import urllib3
import requests
import re
import random
import sys
import numpy
# Saving the reference of the standard output
original_stdout = sys.stdout
header = {}
link = '/search?query='
http = requests.session()
fileName = '/output.txt'
hasCompleted = 0
number = '11'
def search(target):
global targetList
global link
global header
global http
resp = http.request('GET',link+target,headers=header)
match = re.findall('"key":"(.+?)","',resp.text)
if len(match) == 10:
return False
if len(match) == 0:
return " "
elif len(match) < 10:
return resp.text
def treeSearch():
global fileName
global number
global hasCompleted
if hasCompleted == 1:
new = (int(int(number, 36) / 36) + 1)
new = numpy.base_repr(new, 36)
number = new
hasCompleted = 0
if hasCompleted == 0:
x = 0
new = int(int(number, 36)*36)
new = numpy.base_repr(new, 36)
number = new
while x < 37:
new = int(int(number, 36) + 1)
new = numpy.base_repr(new, 36)
number = new
result = search(number)
print(number)
if result:
with open(fileName, 'a+') as f:
sys.stdout = f
print(result)
sys.stdout = original_stdout
x = x + 1
else:
treeSearch()
temp = number
number = (int(int(number, 36) / 36) + 1) #maybe not + 1
print(number)
number = numpy.base_repr(number, 36)
print(number)
result = search(number)
if not result == " ":
new = int(int(number, 36)*36)
new = numpy.base_repr(new, 36)
number = new
hasCompleted = 1
treeSearch()
Here is my output:
111
1111
11111
111111
1111111
11111111
111111111
1111111111
11111111111
111111111111
1111111111111
11111111111111
111111111111111
1111111111111111
1111111111111112
1111111111111113
1111111111111114
1111111111111115
1111111111111116
1111111111111117
1111111111111118
1111111111111119
111111111111111A
111111111111111B
111111111111111C
111111111111111D
111111111111111E
111111111111111F
111111111111111G
111111111111111H
111111111111111I
111111111111111J
111111111111111K
111111111111111L
111111111111111M
111111111111111N
111111111111111O
111111111111111P
111111111111111Q
111111111111111R
111111111111111S
111111111111111T
111111111111111U
111111111111111V
111111111111111W
111111111111111X
111111111111111Y
111111111111111Z
1111111111111120
1111111111111121
6316397706306666889217
11111111110QR5T
11111111110QR5U
11111111110QR5V
11111111110QR5W
11111111110QR5X
11111111110QR5Y
11111111110QR5Z
11111111110QR60
11111111110QR61
11111111110QR62
11111111110QR63
11111111110QR64
11111111110QR65
11111111110QR66
11111111110QR67
11111111110QR68
11111111110QR69
11111111110QR6A
11111111110QR6B
11111111110QR6C
11111111110QR6D
11111111110QR6E
11111111110QR6F
11111111110QR6G
11111111110QR6H
11111111110QR6I
11111111110QR6J
11111111110QR6K
11111111110QR6L
11111111110QR6M
11111111110QR6N
11111111110QR6O
11111111110QR6P
11111111110QR6Q
11111111110QR6R
11111111110QR6S
11111111110QR6T
11111111110QR6U
175455491841851850753
11111111110L4X
11111111110L4Y
11111111110L4Z
11111111110L50
11111111110L51
11111111110L52
11111111110L53
11111111110L54
11111111110L55
11111111110L56
11111111110L57
11111111110L58
11111111110L59
11111111110L5A
11111111110L5B
11111111110L5C
11111111110L5D
11111111110L5E
11111111110L5F
11111111110L5G
11111111110L5H
11111111110L5I
11111111110L5J
11111111110L5K
11111111110L5L
11111111110L5M
11111111110L5N
11111111110L5O
11111111110L5P
11111111110L5Q
11111111110L5R
11111111110L5S
11111111110L5T
11111111110L5U
11111111110L5V
11111111110L5W
11111111110L5X
11111111110L5Y
4873763662273662977
11111111110XT
11111111110XU
11111111110XV
11111111110XW
11111111110XX
11111111110XY
11111111110XZ
11111111110Y0
11111111110Y1
11111111110Y2
11111111110Y3
11111111110Y4
11111111110Y5
11111111110Y6
11111111110Y7
11111111110Y8
11111111110Y9
11111111110YA
11111111110YB
11111111110YC
11111111110YD
11111111110YE
11111111110YF
11111111110YG
11111111110YH
11111111110YI
11111111110YJ
11111111110YK
11111111110YL
11111111110YM
11111111110YN
11111111110YO
11111111110YP
11111111110YQ
11111111110YR
11111111110YS
11111111110YT
11111111110YU
135382323952046193
11111111110X
11111111110Y
11111111110Z
111111111110
111111111111
111111111121
111111111122
111111111123
111111111124
111111111125
111111111126
111111111127
111111111128
111111111129
11111111112A
11111111112B
11111111112C
11111111112D
11111111112E
11111111112F
11111111112G
11111111112H
11111111112I
11111111112J
11111111112K
11111111112L
11111111112M
11111111112N
11111111112O
11111111112P
11111111112Q
11111111112R
11111111112S
11111111112T
11111111112U
11111111112V
11111111112W
11111111112X
11111111112Y
11111111112Z
111111111130
111111111131
3760620109779064
11111111114
111111111141
111111111142
111111111143
111111111144
111111111145
111111111146
111111111147
111111111148
111111111149
11111111114A
11111111114B
11111111114C
11111111114D
11111111114E
11111111114F
11111111114G
11111111114H
11111111114I
11111111114J
11111111114K
11111111114L
11111111114M
11111111114N
11111111114O
11111111114P
11111111114Q
11111111114R
11111111114S
11111111114T
11111111114U
11111111114V
11111111114W
11111111114X
11111111114Y
3760620109779066
11111111116
11111111117
11111111118
11111111119
1111111111A
1111111111B
1111111111C
1111111111D
1111111111E
1111111111F
1111111111G
1111111111H
1111111111I
1111111111J
1111111111K
1111111111L
1111111111M
1111111111N
1111111111O
1111111111P
1111111111Q
1111111111R
1111111111S
1111111111T
1111111111U
1111111111V
1111111111W
1111111111X
1111111111Y
1111111111Z
11111111120
11111111121
11111111122
11111111123
11111111124
11111111125
11111111126
11111111127
104461669716087
1111111113
1111111114
1111111115
1111111116
1111111117
1111111118
1111111119
111111111A
111111111B
111111111C
111111111D
111111111E
111111111F
111111111G
111111111H
111111111I
111111111J
111111111K
111111111L
111111111M
111111111N
111111111O
111111111P
111111111Q
111111111R
111111111S
111111111T
111111111U
111111111V
111111111W
111111111X
111111111Y
111111111Z
1111111120
1111111121
1111111122
1111111123
1111111124
2901713047671
111111113
111111114
111111115
111111116
111111117
111111118
111111119
11111111A
11111111B
11111111C
11111111D
11111111E
11111111F
11111111G
11111111H
11111111I
11111111J
11111111K
11111111L
11111111M
11111111N
11111111O
11111111P
11111111Q
11111111R
11111111S
11111111T
11111111U
11111111V
11111111W
11111111X
11111111Y
11111111Z
111111120
111111121
111111122
111111123
111111124
80603140215
11111113
11111114
11111115
11111116
11111117
11111118
11111119
1111111A
1111111B
1111111C
1111111D
1111111E
1111111F
1111111G
1111111H
1111111I
1111111J
1111111K
1111111L
1111111M
1111111N
1111111O
1111111P
1111111Q
1111111R
1111111S
1111111T
1111111U
1111111V
1111111W
1111111X
1111111Y
1111111Z
11111120
11111121
11111122
11111123
11111124
2238976119
1111113
11111131
11111132
11111133
11111134
11111135
11111136
11111137
11111138
11111139
1111113A
1111113B
1111113C
1111113D
1111113E
1111113F
1111113G
1111113H
1111113I
1111113J
1111113K
1111113L
1111113M
1111113N
1111113O
1111113P
1111113Q
1111113R
1111113S
1111113T
1111113U
1111113V
1111113W
1111113X
1111113Y
1111113Z
11111140
11111141
2238976121
1111115
11111151
11111152
11111153
11111154
11111155
11111156
11111157
11111158
11111159
1111115A
1111115B
1111115C
1111115D
1111115E
1111115F
1111115G
1111115H
1111115I
1111115J
1111115K
1111115L
1111115M
1111115N
1111115O
1111115P
1111115Q
1111115R
1111115S
1111115T
1111115U
1111115V
1111115W
1111115X
1111115Y
1111115Z
11111160
11111161
2238976123
1111117
11111171
11111172
11111173
11111174
11111175
11111176
11111177
11111178
11111179
1111117A
1111117B
1111117C
1111117D
1111117E
1111117F
1111117G
1111117H
1111117I
1111117J
1111117K
1111117L
1111117M
1111117N
1111117O
1111117P
1111117Q
1111117R
1111117S
1111117T
1111117U
1111117V
1111117W
1111117X
1111117Y
1111117Z
11111180
11111181
2238976125
1111119
111111A
111111B
111111C
111111D
111111E
111111F
111111G
111111H
111111I
111111J
111111K
111111L
111111M
111111N
111111O
111111P
111111Q
111111R
111111S
111111T
111111U
111111V
111111W
111111X
111111Y
111111Z
1111120
1111121
1111122
1111123
1111124
1111125
1111126
1111127
1111128
1111129
111112A
62193783
111113
1111131
1111132
1111133
1111134
1111135
1111136
1111137
1111138
1111139
111113A
111113B
111113C
111113D
111113E
111113F
111113G
111113H
111113I
111113J
111113K
111113L
111113M
111113N
111113O
111113P
111113Q
111113R
111113S
111113T
111113U
111113V
111113W
111113X
111113Y
111113Z
1111140
1111141
62193785
111115
My code traverses in only deeper once then comes out, I will keep working on my code however I am hoping to find an easier solution or possibly a library that can perform this style of search. Thanks!

Python Binary and Regular String Confusion

I am sure this is a fairly noob question, I have googled, and cannot find a straight answer, but I may be asking the wrong thing... I am trying to make an Out Of Box Configuration script, and all the questions that need answered are stored in a file called pass.ini. When i get user input from getstr (using curses) when it populates my files, they all have b'variable string' as their values. when I try to do a strip command, I get b'riable strin'. when I do a str(variable) it gets the same issue. I saw where b'<variable_string>' can be a sign that it was in bytecode instead of decoded. so I tried a decode command and that failed as 'str object has no attribute 'decode' I have it writing out via ConfigParser, and to a separate file just as a file.write. Right now everything is commented out, I am out of ideas.
Here is the info gathering module:
wrapper(CommitChanges)
curses.echo()
stdscr.addstr( 8, 19, config.CIP , curses.color_pair(3) )
config.CIP = stdscr.getstr( 8, 19, 15)
stdscr.addstr( 9, 19, config.CSM , curses.color_pair(3) )
config.CSM = stdscr.getstr( 9, 19, 15)
stdscr.addstr( 10, 19, config.CGW , curses.color_pair(3) )
config.CGW = stdscr.getstr(10, 19, 15)
stdscr.addstr( 11, 19, config.CD1 , curses.color_pair(3) )
config.CD1 = stdscr.getstr(11, 19, 15)
stdscr.addstr( 12, 19, config.CD2 , curses.color_pair(3) )
config.CD2 = stdscr.getstr(12, 19, 15)
stdscr.addstr( 13, 19, config.CNTP, curses.color_pair(3) )
config.CNTP = stdscr.getstr(13, 19, 15)
stdscr.addstr( 16, 19, config.CHN , curses.color_pair(3) )
config.CHN = stdscr.getstr(16, 19, 15)
stdscr.addstr( 14, 19, config.CID , curses.color_pair(3) )
config.CID = stdscr.getstr(14, 19, 15)
stdscr.addstr( 15, 19, config.CS , curses.color_pair(3) )
config.CS = stdscr.getstr(15, 19, 15)
This is the file output module
def CommitChanges():
MOP = "X"
Config['Array=all']['PTLIP'] = a
Config['Array=all']['PTLSM'] = config.CSM.decode('utf-8')
Config['Array=all']['PTLGW'] = config.CGW.decode('utf-8')
Config['Array=all']['PTLD1'] = config.CD1.decode('utf-8')
Config['Array=all']['PTLD2'] = config.CD2.decode('utf-8')
Config['Array=all']['PTLNTP'] = config.CNTP.decode('utf-8')
Config['Array=all']['PTLIF'] = config.CIFN.decode('utf-8')
Config['Array=all']['PTLHSTNM'] = config.CHN.decode('utf-8')
Config['Array=all']['PTLMOB'] = config.CMOB.decode('utf-8')
Config['Array=all']['customerid'] = config.CID.decode('utf-8')
Config['Array=all']['site'] = config.CS.decode('utf-8')
with open('/opt/passp/pass.ini', 'w') as passini:
Config.write(passini, space_around_delimiters=False)
tpass= open('./pass.b', 'w')
tpass.write("[Array=All]"+ "\n")
tpass.write("ptlip="+ a + "\n")
tpass.write("ptlsm="+ config.CSM.decode('utf-8') +"\n")
tpass.write("ptlgw="+ config.CGW.decode('utf-8') + "\n")
tpass.write("ptld1="+ config.CD1.decode('utf-8') + "\n")
tpass.write("ptld2="+ config.CD2.decode('utf-8') + "\n")
tpass.write("ptlntp="+ config.CNTPdecode('utf-8') + "\n")
tpass.write("ptlif="+ config.CIFNdecode('utf-8') + "\n")
tpass.write("ptldhstnm="+ config.CHNdecode('utf-8') + "\n")
tpass.write("ptlmob="+ config.CMOBdecode('utf-8') + "\n")
tpass.write("customerid="+ config.CIDdecode('utf-8') + "\n")
tpass.write("site="+ config.CSdecode('utf-8') + "\n")
#if Backupfiles():
textchanges()
return
Here is the file save output created by ConfigParser
[Array=all]
ptlip=b'123'
ptlsm=b'321'
ptlgw=b'111'
ptld1=b'222'
ptld2=b'333'
ptlntp=b'444'
ptlif=s19
ptlhstnm=b'555'
ptlmob=
customerid=b'666'
site=b'777'
It perfectly matches when I do a direct write (they were from two different runs, but even with empty data it has the wrapper.
Interesting notice here, 'ptlif' is gathered from finding the interface name, it isn't handled by user input, so it has to be how the config.XXXX variables are stored.
[Array=All]
ptlip=b''
ptlsm=b''
ptlgw=b''
ptld1=b''
ptld2=b''
ptlntp=b''
ptlif=s19
ptldhstnm=b''
ptlmob=
customerid=b''
site=b''
Ok, so I figured out what my problem was.
ncurses getstr returns a binary value, and I was sending it to a string literal variable.
I created a new set of variables, set them up as
cipb = b'foo'
cipb = stdscr.getstr( y, x, len )
config.CIP = cipb.decode()
That fixed it. Hopefully this might help someone else out with a similar issue.

Python 3.4 Syntax error and how to fix it

While the following line is accepted on Python 3.6, On Python 3.4 I am getting a syntax error:
struct.pack_into('q' * len(GeoFence_lat_list)*2,buff,264,*GeoFence_lat_list, *GeoFence_lon_list)
Where GeoFence_lon_list is an array declared as:
Geo_Fence_list = []
GeoFence_lat_list = []
GeoFence_lon_list = []
Here is more code to review:
if (Polygon_available_size == 0):
buff = ctypes.create_string_buffer(workzone_size)
struct.pack_into('q' * 33, buff, 0, Speed_limit, Speed_Zone_lat, Speed_Zone_longi, Speed_Zone_heading_radians,
Speed_Zone_ITIS_CODE, Speed_Zone_2_lat, Speed_Zone_2_longi, Speed_Zone_2_heading_radians, Speed_Zone_2_ITIS_CODE,G20_lat, G20_longi,
G20_heading_radians, G20_ITIS_CODE,W20_lat, W20_longi, W20_heading_radians ,W20_ITIS_CODE,W21_5BR_lat,W21_5BR_longi, W21_5BR_heading_radians,
W21_5BR_ITIS_CODE,W21_5AR_lat,W21_5AR_longi, W21_5AR_heading_radians, W21_5AR_ITIS_CODE,First_Taper_lat,First_Taper_longi,
Last_Taper_lat, Last_Taper_longi, 2020, 2456,60, Polygon_available_size)
elif (int(Polygon_available_size) > 0):
geo_fence_size = struct.calcsize('q' * len(GeoFence_lat_list)*2)
#print("geo_fence_size", geo_fence_size)
workzone_size = workzone_size + geo_fence_size
buff = ctypes.create_string_buffer(workzone_size)
struct.pack_into('q' * 33, buff, 0, Speed_limit, Speed_Zone_lat, Speed_Zone_longi, Speed_Zone_heading_radians,
Speed_Zone_ITIS_CODE, Speed_Zone_2_lat, Speed_Zone_2_longi, Speed_Zone_2_heading_radians, Speed_Zone_2_ITIS_CODE,G20_lat, G20_longi,
G20_heading_radians, G20_ITIS_CODE,W20_lat, W20_longi, W20_heading_radians ,W20_ITIS_CODE,W21_5BR_lat,W21_5BR_longi, W21_5BR_heading_radians,
W21_5BR_ITIS_CODE,W21_5AR_lat,W21_5AR_longi, W21_5AR_heading_radians, W21_5AR_ITIS_CODE,First_Taper_lat,First_Taper_longi,
Last_Taper_lat, Last_Taper_longi, 2020, 2456,60, Polygon_available_size)
struct.pack_into('q', * len(GeoFence_lat_list)*2,buff,264,*GeoFence_lat_list, *GeoFence_lon_list)
GeoFence_lat_list.clear()
GeoFence_lon_list.clear()
packed_flag = 1
Python 3.4 only allows one unpacked argument per call.
Your original code is using multiple unpackings simply to append multiple lists into the byte buffer. You can achieve the same thing simply by calling struct.pack_into() multiple times, with appropriate offsets for each part.
start = 264
struct.pack_into(str(len(GeoFence_lat_list)) + 'q', buff, start, *GeoFence_lat_list);
start += 8 * len(GeoFence_lat_list)
struct.pack_into(str(len(GeoFence_lon_list)) + 'q', buff, start, *GeoFence_lon_list);

Porting Node.js script to Python

I want to port this Node.js script to control a Sky box over into Python, https://github.com/dalhundal/sky-remote/blob/master/sky-remote.js
I've gone through and done the best that I can do, code is below;
import time, math, socket, struct, time
from array import array
#sky q port 5900
class remote:
commands={"power": 0, "select": 1, "backup": 2, "dismiss": 2, "channelup": 6, "channeldown": 7, "interactive": 8, "sidebar": 8, "help": 9, "services": 10, "search": 10, "tvguide": 11, "home": 11, "i": 14, "text": 15, "up": 16, "down": 17, "left": 18, "right": 19, "red": 32, "green": 33, "yellow": 34, "blue": 35, 0: 48, 1: 49, 2: 50, 3: 51, 4: 52, 5: 53, 6: 54, 7: 55, 8: 56, 9: 57, "play": 64, "pause": 65, "stop": 66, "record": 67, "fastforward": 69, "rewind": 71, "boxoffice": 240, "sky": 241}
connectTimeout = 1000;
def __init__(self, ip, port=49160):
self.ip=ip
self.port=port
def showCommands(self):
for command, value in self.commands.iteritems():
print str(command)+ " : "+str(value)
def getCommand(self, code):
try:
return self.commands[code]
except:
print "Error: command '"+code+"' is not valid"
return False
def press (self, sequence):
if isinstance(sequence, list):
for item in sequence:
toSend=self.getCommand(item)
if toSend:
self.sendCommand(toSend)
time.sleep(0.5)
else:
toSend=self.getCommand(sequence)
if toSend:
self.sendCommand(toSend)
def sendCommand(self, code):
commandBytes = array('l', [4,1,0,0,0,0, int(math.floor(224 + (code/16))), code % 16])
try:
client=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
return
try:
client.connect((self.ip, self.port))
except:
print "Failed to connect to client"
return
l=12
timeout=time.time()+self.connectTimeout
while 1:
data=client.recv(1024)
data=data
if len(data)<24:
client.sendall(data[0:l])
l=1
else:
client.sendall(buffer(commandBytes))
commandBytes[1]=0
client.sendall(buffer(commandBytes))
client.close()
break
if time.time() > timeout:
print "timeout error"
break
I think the issue is how I form the buffers? I'm not entirely sure as this is the first time I've dealt with buffers.
Having read through the Node.js documentation on new Buffer, it looks like it creates an array of Octets, whereas what I have is an array of ints, I may be wrong but an Octet is 8bits whilst an int is 4bits, I've tried changing the array to long and double, but this doesn't seem to resolve the issue
Having had the time to have a quick read through on how Node.js handles buffers and buffers in general and it looks like what I thought was right.
Change;
commandBytes = array('l', [4,1,0,0,0,0, int(math.floor(224 + (code/16))), code % 16])
to;
commandBytes = bytearray([4,1,0,0,0,0, int(math.floor(224 + (code/16))), code % 16])
Also, then just pass the bytearray;
client.sendall(commandBytes)
instead of;
client.sendall(buffer(commandBytes))

Python Procedures

I have developed a graphical Library (in Pascal) and wanted to use it with several other languages. I have tried it with Python and it works.
Here part of the working code:
#file fpgui-test.py
from ctypes import*
fpgui = cdll.LoadLibrary("fpgui-32.dll")
fpgui.fpgInitialize()
fpgui.fpgFormCreate(0, -1)
fpgui.fpgFormSetPosition(0, 300,100,400,200)
fpgui.fpgFormWindowTitle(0, 'Hello world!')
fpgui.fpgButtonCreate(0,0,-1) ;
fpgui.fpgButtonSetPosition(0,0, 15, 10 , 150 , 40)
fpgui.fpgButtonSetText(0,0, 'BUTTON1')
fpgui.fpgButtonCreate(0,1,-1) ;
fpgui.fpgButtonSetPosition(0,1, 15, 70 , 150, 40)
fpgui.fpgButtonSetText(0,1, 'Clickme')
fpgui.fpgFormShow(0)
fpgui.fpgRun()
But now I want to add some procedures. In Pascal a procedure is declared like this:
fpgFormOnClick(index : integer ;TheProc : procedure);
And used like this:
fpgFormOnClick(0,#TheProc); => how can you translate that in Python ?
And, for example, if TheProc procedure is like that in Pascal, how do I do it in Python?
procedure TheProc;
begin
fpgButtonSetText(0,1,'Test');
end;
Here is how you can do it.
# here we register the events
events={}
# registation function for events
def fpgFormOnClick(index, procedure):
events[index] = procedure
# emulates fireing of events
def fire(index):
# check if we have an event registered
if index in events:
# call the registered function/procedure
events[index]()
def TheProc():
# if you use python 3 use
# print("TheProc")
print "TheProc"
# register the function
fpgFormOnClick(0,TheProc)
# and fire it
fire(0)
fire(1)
Or is your question about how to get the python function called by the DLL?
I have found the solution - it was in "CallBack function" in help-doc.
#file fpgui-test.py
from ctypes import*
def TheProc():
fpgui.fpgFormWindowTitle(0, 'Boum')
return 0
def TheProcBut0():
fpgui.fpgButtonSetText(0,0, 'Boum also')
return 0
def TheProcBut1():
fpgui.fpgButtonSetText(0,1, 'Boum too')
return 0
CMPFUNC = CFUNCTYPE(c_int)
TheProcF = CMPFUNC(TheProc)
TheProcB0 = CMPFUNC(TheProcBut0)
TheProcB1 = CMPFUNC(TheProcBut1)
fpgui = cdll.LoadLibrary("fpgui-32.dll")
fpgui.fpgInitialize()
fpgui.fpgSetStyle('Demo Style')
fpgui.fpgFormCreate(0, -1)
fpgui.fpgFormSetPosition(0, 300,100,400,200)
fpgui.fpgFormWindowTitle(0, 'Hello world!')
fpgui.fpgFormOnClick(0,TheProcF)
fpgui.fpgButtonCreate(0,0,-1) ;
fpgui.fpgButtonSetPosition(0,0, 15, 10 , 150 , 40)
fpgui.fpgButtonSetText(0,0, 'BUTTON1')
fpgui.fpgButtonOnClick(0,0,TheProcB0)
fpgui.fpgButtonCreate(0,1,-1) ;
fpgui.fpgButtonSetPosition(0,1, 15, 70 , 150, 40)
fpgui.fpgButtonSetText(0,1, 'Clickme')
fpgui.fpgButtonOnClick(0,1,TheProcB1)
fpgui.fpgFormShow(0)
fpgui.fpgRun()

Categories

Resources