首页 > 解决方案 > TypeError:只能将str(不是“字节”)连接到str Python3.6.8

问题描述

我从网上得到的脚本

#! /usr/bin/python
print ("\n*********************************************************************")
print ("Cisco IOU License Generator - Kal 2011, python port of 2006 C version")
import os
import socket
import hashlib
import struct
# get the host id and host name to calculate the hostkey
hostid=os.popen("hostid").read().strip()
hostname = socket.gethostname()
ioukey=int(hostid,16)
for x in hostname:
 ioukey = ioukey + ord(x)
print ("hostid=" + hostid +", hostname="+ hostname + ", ioukey=" + hex(ioukey)[2:])
# create the license using md5sum
iouPad1='\x4B\x58\x21\x81\x56\x7B\x0D\xF3\x21\x43\x9B\x7E\xAC\x1D\xE6\x8A'
iouPad2='\x80' + 39*'\0'
md5input=iouPad1 + iouPad2 + struct.pack('!L', ioukey) + iouPad1
iouLicense=hashlib.md5(md5input).hexdigest()[:16]
# add license info to $HOME/.iourc
print ("\n*********************************************************************")
print ("Create the license file $HOME/.iourc with this command:")
print (" echo -e '[license]\\n" + hostname + " = " + iouLicense + ";'" + " | tee $HOME/.iourc ")
print ("\nThe command adds the following text to $HOME/.iourc:")
print ("[license]\n" + hostname + " = " + iouLicense + ";")
# disable phone home feature
print ("\n*********************************************************************")
print ("Disable the phone home feature with this command:")
print (" grep -q -F '127.0.0.1 xml.cisco.com' /etc/hosts || echo '127.0.0.1 xml.cisco.com' | sudo tee -a /etc/hosts")
print ("\nThe command adds the following text to /etc/hosts:")
print ("127.0.0.1 xml.cisco.com")
print ("\n*********************************************************************")

出于某种原因,我遇到了这个错误

Traceback (most recent call last):  File "main.py", line 18, in <module>
    md5input=iouPad1 + iouPad2 + struct.pack('!L', ioukey) + iouPad1
TypeError: can only concatenate str (not "bytes") to str

我正在尝试在我的 GNS3 的 linux 发行版中使用此脚本,但我无法在 Python v3.6.8 中运行此脚本。这个问题有什么解决办法吗?有人可以给我发送正确的脚本吗?

标签: pythonstringbytepython-3.6

解决方案


您可以尝试替换
md5input=iouPad1 + iouPad2 + struct.pack('!L', ioukey) + iouPad1
md5input=iouPad1 + iouPad2 + struct.pack('!L', ioukey).decode() + iouPad1.


推荐阅读