You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to set sysDescr.0 use set,but I don't know how to convert my string it to Type
oid = "1.3.6.1.2.1.1.1.0"
#how to conver a python string to Utf8String
value = "snmp test"
result = set(host, write_community, oid, value, port, timeout)
The text was updated successfully, but these errors were encountered:
You can set it by creating an puresnmp.x690.types.OctetString instance and encoding the value to UTF-8:
from puresnmp.x690.types import OctetString
encoded_value = OctetString(value.encode('utf8'))
Let me know if this will work and I will close this issue.
Python 2 vs Python 3
The solution above is for Python 3! If you already use Python 3 you are fine and don't need to worry about byte details!
In Python 2, string values without "u" prefix are already considered bytes and you may run into double-encoding errors in the worst case. You have to be 100% certain that the value you encode into "utf8" is an instance of unicode. To do that, if the value is hard-coded in the script, simply prefix the string with u (u"tha-value") or decode it if it comes from user-input. If you have questions, don't hesitate to ask.
sysDescr is documented to be ASCII
A warning: According to RFC-1213 Section 3.2 the sysDescr field is limited to US-ASCII. Storing UTF-8 values is possible because the limit is purely conventional. And I have myself encountered devices which store non-ASCII values in those fields. So make sure that this will not break anything in unexpected ways.
If you want to be safe, and don't want to lose any data, you can use one of the replacement error-handlers for string encoding. For example, I quite like xmlcharrefreplace:
from puresnmp.x690.types import OctetString
encoded_value = OctetString(value.encode('ascii', errors='xmlcharrefreplace'))
If you want to use US-ASCII you can just wrap the string-value in OctetString without encoding. The encoding is automatic in puresnmp if not already encoded (see
I want to set sysDescr.0 use set,but I don't know how to convert my string it to Type
The text was updated successfully, but these errors were encountered: