我有一個IP地址列表,我telnet到並從中收集數據。我把這些數據分成兩個變量。然後我想要將變量中的數據打印到HTML表格中。它在Python 2中工作,但不在Python 3中。它給了我以下錯誤:Can't convert 'bytes' object to str implicitly
。我看到其他人給出瞭解釋字節碼vs字符串代碼,但列表呢?如果可以的話請幫忙。Python2到python3列表和字節與字符串
#!/usr/bin/python3
import cgi, cgitb
import telnetlib
import re
import socket
user = 'usr'
password = 'pwd'
print ("Content-type:text/html\r\n\r\n")
print ("<html>")
print ("<head>")
print ("<title>Locating IP Addresses</title>")
print ("<link href=\"/styles/main.css\" type=\"text/css\" rel=\"stylesheet\" >")
print ("</head>")
print ("<body>")
for count in ["10.1.1.4", "10.1.1.3", "10.1.1.2"]:
server = (count)
try:
tn = telnetlib.Telnet(server)
tn.read_until(b"ogin")
tn.write(user.encode('ascii') + b"\r\n")
tn.read_until(b"assword")
tn.write(password.encode('ascii') + b"\r\n")
tn.write(b"environment no more\r\n")
tn.write(b"configure\r\n")
tn.write(b"router\r\n")
tn.write(b"info\r\n")
tn.write(b"logout\r\n")
output = (tn.read_all())
interfaces = (re.findall(b'interface\s\"(.+)\"', output))
ipaddr = (re.findall(b'address\s(.+)/', output))
print ("<table>")
print ("<tr>")
print ("<th class=\"bld\">%s</th>" % (server))
print ("</tr>")
for i,j in zip(interfaces, ipaddr):
print ("<tr>")
print (("<td class=\"sn\">"+j+"</td>" "<td class=\"prt\">"+i+"</td>"))
except socket.error:
print ("communication error with " + server)
print ("</body>")
print ("</html>")
我或許可以幫忙,但是你可以張貼完整的錯誤信息,即包括行號?我看不到錯誤發生在哪裏。 – refi64