[問題] 請問paramiko讀取遠端big5問題

看板Python作者 (曼秀雷敦)時間6年前 (2018/09/13 17:30), 6年前編輯推噓4(404)
留言8則, 3人參與, 6年前最新討論串1/1
import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='192.168.1.1, port=22, username='root', password='password', timeout=3) command='df -h | awk \'NR>1\'' stdin, stdout, stderr = ssh.exec_command(command) y = stdout.readlines() for tmp in y: print(tmp) 事情是這樣的 本機端是utf8 centos7環境 遠端是 big5 centos 5 這樣在print資訊的時候總是出現 nicodeDecodeError: 'utf-8' codec can't decode byte 0xa5 in position 19: invalid start byte 不管如何encode 或 decode都沒辦法成功轉換 請問我要朝甚麼方向找答案 感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.124.109.173 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1536831037.A.9CB.html

09/13 21:22, 6年前 , 1F
用decode('big5')解碼
09/13 21:22, 1F

09/14 09:26, 6年前 , 2F
試過了 卻誤碼一樣
09/14 09:26, 2F

09/14 09:27, 6年前 , 3F
錯誤碼
09/14 09:27, 3F

09/14 11:01, 6年前 , 4F
stdout=io.TextIOWrapper(stdout, encoding='Big5')試試
09/14 11:01, 4F

09/14 13:01, 6年前 , 5F
樓上大大 可以了耶...感謝!!!!
09/14 13:01, 5F

09/14 13:34, 6年前 , 6F
不過要不要試試psutil https://tinyurl.com/y9bepsqq
09/14 13:34, 6F

09/14 13:34, 6年前 , 7F
不用自己parse
09/14 13:34, 7F
感謝推薦 psutil 不過我的環境比較複雜 不想要每台被監測的系統都寫script 比較希望一台server去問資料回來解析 ※ 編輯: phoenixcx (59.124.109.173), 09/14/2018 13:56:25

09/14 18:53, 6年前 , 8F
學習了
09/14 18:53, 8F
文章代碼(AID): #1RcYuzdB (Python)
文章代碼(AID): #1RcYuzdB (Python)