Skip to content

Commit 5862d60

Browse files
committed
Fix to support Autodesk Rivet and IronPython 2.7.3 or earlier
1 parent 780355f commit 5862d60

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Python docs can be found here: https://help.cryptolens.io/api/python/
66

77
**Autodesk Maya**: The Python2 version needs to be used, as described [here](https://cryptolens.io/2019/07/autodesk-maya-plugin-software-licensing/).
88

9+
**Autodesk Revit / Iron Python 2.7.3**: The Python2 version needs to be used with `HelperMethods.ironpython2730_legacy = True`.
10+
911
## Installation
1012

1113
### Python 3
@@ -21,6 +23,12 @@ Please copy `cryptolens_python2.py` file into your project folder. The entire li
2123
from cryptolens_python2 import *
2224
```
2325

26+
If you create a plugin for Autodesk Rivet or use IronPython 2.7.3 or earlier, please also add the line below right after the import:
27+
28+
```
29+
HelperMethods.ironpython2730_legacy = True
30+
```
31+
2432
## Example
2533

2634
### Key verification

cryptolens_python2.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
class HelperMethods:
2424

2525
server_address = "https://app.cryptolens.io/api/"
26+
ironpython2730_legacy = False
2627

2728
@staticmethod
2829
def get_SHA256(string):
@@ -118,10 +119,41 @@ def send_request(method, params):
118119
119120
method: the path of the method, eg. key/activate
120121
params: a dictionary of parameters
121-
"""
122-
return urllib2.urlopen(HelperMethods.server_address + method, \
123-
urllib.urlencode(params)).read().decode("utf-8")
124-
122+
"""
123+
124+
if HelperMethods.ironpython2730_legacy:
125+
return HelperMethods.send_request_ironpythonlegacy(HelperMethods.server_address + method, \
126+
urllib.urlencode(params))
127+
else:
128+
return urllib2.urlopen(HelperMethods.server_address + method, \
129+
urllib.urlencode(params)).read().decode("utf-8")
130+
131+
@staticmethod
132+
def send_request_ironpythonlegacy(uri, parameters):
133+
"""
134+
IronPython 2.7.3 and earlier has a built in problem with
135+
urlib2 library when verifying certificates. This code calls a .NET
136+
library instead.
137+
"""
138+
from System.Net import WebRequest
139+
from System.IO import StreamReader
140+
from System.Text import Encoding
141+
142+
request = WebRequest.Create(uri)
143+
144+
request.ContentType = "application/x-www-form-urlencoded"
145+
request.Method = "POST" #work for post
146+
bytes = Encoding.ASCII.GetBytes(parameters)
147+
request.ContentLength = bytes.Length
148+
reqStream = request.GetRequestStream()
149+
reqStream.Write(bytes, 0, bytes.Length)
150+
reqStream.Close()
151+
152+
response = request.GetResponse()
153+
result = StreamReader(response.GetResponseStream()).ReadToEnd()
154+
return result
155+
156+
125157
@staticmethod
126158
def start_process(command):
127159

@@ -187,8 +219,8 @@ def activate(token, rsa_pub_key, product_id, key, machine_code, fields_to_return
187219

188220
response = Response("","",0,"")
189221

190-
try:
191-
response = Response.from_string(HelperMethods.send_request("key/activate", {"token":token,\
222+
#try:
223+
response = Response.from_string(HelperMethods.send_request("key/activate", {"token":token,\
192224
"ProductId":product_id,\
193225
"key":key,\
194226
"MachineCode":machine_code,\
@@ -198,8 +230,8 @@ def activate(token, rsa_pub_key, product_id, key, machine_code, fields_to_return
198230
"MaxOverdraft": max_overdraft,\
199231
"Sign":"True",\
200232
"SignMethod":1}))
201-
except Exception:
202-
return (None, "Could not contact the server.")
233+
#except Exception:
234+
# return (None, "Could not contact the server.")
203235

204236
pubkey = RSAPublicKey.from_string(rsa_pub_key)
205237

test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
from licensing.models import *
99
from licensing.methods import Key, Helpers
1010

11+
#from cryptolens_python2 import *
1112
pubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
12-
13+
#HelperMethods.ironpython2730_legacy = True
1314
res = Key.activate(token="WyIyNzMyIiwiYmx6NlJ6ZzdaWjFScmxFVFNCc283YTJyUG5kQURMZ0hucW1YdUZxKyJd",\
1415
rsa_pub_key=pubKey,\
1516
product_id=3349, key="ICVLD-VVSZR-ZTICT-YKGXL", machine_code=Helpers.GetMachineCode())

0 commit comments

Comments
 (0)