'How to get attribute value from Extension object (Python cryptography)?
I want to access name attribute from Extension object.
from cryptography import x509
from cryptography.x509.oid import ExtensionOID
l_certificate = rgetattr(self.l_config_json, 'web_interface.certificate.certificate')
d_certificate = x509.load_pem_x509_certificate(str.encode(l_certificate))
ext = d_certificate.extensions.get_extension_for_oid(ExtensionOID.EXTENDED_KEY_USAGE)
extendedKeyUsage = ext.value
print(extendedKeyUsage)
Result:
<ExtendedKeyUsage([<ObjectIdentifier(oid=1.3.6.1.5.5.7.3.2, name=clientAuth)>, <ObjectIdentifier(oid=1.3.6.1.5.5.7.3.1, name=serverAuth)>])>
How to access name attributes(clientAuth and serverAuth) from result??
Solution 1:[1]
If I understand the goal, you are just wondering how to access the attribute values? These can be derived by looking at the class definitions in site-packages/cryptography/x509/extensions.py and is a foundational skill (i.e. read the source code).
What I do is pretty straight forward:
# common to cryptography.x509.extensions.ExtensionType
data = {
'critical': ext.critical,
'name': ext.oid._name
}
# extension specific attributes
if isinstance(ext.value, cryptography.x509.extensions.ExtendedKeyUsage):
data['key_usages'] = [x._name for x in ext.value or []]
which produced
{'critical': False,
'key_usages': ['serverAuth', 'clientAuth'],
'name': 'extendedKeyUsage'}
instead of the repr you have, and cannot parse I guess?
Solution 2:[2]
The response you are getting and storing in 'ext' is a list. Access the individual items in the list by either iterating through it or by directly referencing its location. For eg: for "clientAuth" try using ext[0].value
extendedKeyUsage = ext[0].value
print(extendedKeyUsage)
Solution 3:[3]
I hope below code is the solution for your problem. Please note that I have use os.mkdirs() to create new directory. but you can try with Path.mkdir()
I have created four text file. out of 4, three text file have strings 2012. and one text file contains string 2020 and three text file contains 2012
Input files:
- ff1.txt (contains string
"2012") - ff2.txt (doesn't contains string
"2012") - ff_blah1.txt (contains string
"2012") - ff_blah1_blah2.txt (contains string
"2012")
I have broken down problem statement in steps
- find the files which has name starts with
"ff" - after finding files, open all the files and search string
"2012" - create new folder
"2012"if it doesn't exists - move all files that we find at step2 into new created folder
"2012"
import os import shutil source_path = '/Users/MacJJ271123/Desktop/PY_Script/' list_of_files = os.listdir(source_path) def move_file(): for file in list_of_files: if file.startswith("ff"): if '2012' in open(file).read(): if not os.path.exists(os.path.join(source_path, "2012")): os.mkdir(os.path.join(source_path, "2012")) shutil.move(os.path.join(source_path,file), os.path.join(source_path,"2012",file)) print(file) move_file()
Output:
MacJ271123-MacBook-Air~ # python3 move_files_stack_oveflow.py
ff_blah1_blah2.txt
ff_blah1.txt
ff1.txt
MacJJ271123-MacBook-Air~ #
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
| Solution 2 | Surendranatha Reddy T |
| Solution 3 | Jignesh23 |
