'Python encoding issue, Network adapter not recognised
Here is my piece of python code scratch.
import os
print 'netsh interface ip set address name="' + adapter + '" static '+ staticaddr +' 255.255.255.0 192.168.1.1'
Until this point all is fine the adapter is included within my drivers.
Now when i run this (as an administrator)
os.system('netsh interface ipv4 set address name="' + adapter + '" static '+ staticaddr +' 255.255.255.0 192.168.1.1')
It fires this error:
La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte.
Which means the command's syntax isn't correct.
I tried os.system('netsh interface ipv4 set address name="' + adapter.encode('ascii','ignore') + '" static '+ staticaddr +' 255.255.255.0 192.168.1.1')
Now this kind of exception occurs:
'ascii' codec can't decode byte 0x82 in position 11: ordinal not in range(128)
Where is the problem exactly ?
My network adapter's name is : Connexion réseau sans fil
Solution 1:[1]
adapter.encode('ascii','ignore')
raises UnicodeDecodeError
because adapter
is a non-ascii str. To encode it (i.e. convert from unicode to str) Python first tried to decode it (i.e. convert from str to unicode) and failed (adapter
is non-ascii).
Switch to unicode completely:
print (u'netsh interface ip set address name="' + adapter.decode('latin1') + u'" static '+ staticaddr.decode('ascii') + u' 255.255.255.0 192.168.1.1').encode('latin1')
Solution 2:[2]
I've encountered a similar problem in my end. I have a script that lists all the previously connected wi-fi networks and their passwords. One network specifically ("Gestão TI") was giving me a headache, beacuse when I ran the command, the name came back as "GestÆo TI", and the following command couldn't run, because the name changed. I tried a few different encodings, such as UTF-8, Latin-1 and others, with no success. A frind of mine pointed out to me that maybe the problem was with the netsh encoding when it was exporting the names. As the netsh is fairly old, he suggested I tried using the old latin encoding (CP850) and it finally worked...
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 | phd |
Solution 2 | Conrado Pellegrini |