Python:AttributeError:';int';对象没有属性';伊萨尔法';
我希望使用客户机/服务器在python中创建凯撒密码函数。客户端发送带有密钥的消息,服务器对其进行加密。 服务器代码:Python:AttributeError:';int';对象没有属性';伊萨尔法';,python,client-server,caesar-cipher,Python,Client Server,Caesar Cipher,我希望使用客户机/服务器在python中创建凯撒密码函数。客户端发送带有密钥的消息,服务器对其进行加密。 服务器代码: import socket def getCaesar(message, key): result="" for l in message: if l.isalpha(): num = ord(l) num += key if l.isupper():
import socket
def getCaesar(message, key):
result=""
for l in message:
if l.isalpha():
num = ord(l)
num += key
if l.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
elif l.islower():
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
result += chr(num)
else:
result += l
return result
serverSock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host=socket.gethostname()
port=4000
serverSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serverSock.bind((host,port))
serverSock.listen(5)
print("Listenting for requests")
while True:
s,addr=serverSock.accept()
print("Got connection from ",addr)
print("Receiving...")
message=s.recv(1024)
key=s.recv(1024)
resp=getCaesar(message, key)
print('Ciphertext: ')
print(resp)
serverSock.close()
导入套接字
def getCaesar(消息,键):
result=“”
对于消息中的l:
如果l.isalpha():
num=ord(l)
num+=键
如果l.isupper():
如果num>ord('Z'):
num-=26
elif numord('z'):
num-=26
elif num
不断被调用的行是第6行:“if l isalpha():”并给出错误:
AttributeError:“int”对象没有属性“isalpha”。
这个错误意味着什么
客户端程序:
import socket
def getMessage():
print('Enter your message:')
return input()
def getKey():
key = 0
while True:
print('Enter the key number (1-%s)' % (26))
key = int(input())
if (key >= 1 and key <= 26):
return key
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host=socket.gethostname()
port=4000
s.connect((host,port))
message = getMessage()
key = getKey()
message=message.encode()
s.send(message)
s.send(bytes(key))
cipher= s.recv(1024)
print('Ciphertext: ')
print(cipher)
s.close()
导入套接字
def getMessage():
打印('输入您的消息:')
返回输入()
def getKey():
键=0
尽管如此:
打印('输入密钥编号(1-%s)'(26))
key=int(输入())
在python 3中,if(key>=1和key,返回一个不可变的序列。每个元素的类型为int
,范围为[0255]。是str
的方法,而不是int
的方法
如果要将服务器响应视为字符串,可以对字节进行解码
string = response.decode('utf-8')
for c in string:
if c.isalpha():
[...]
在python 3中,返回一个不可变的序列。每个元素的类型为int
,范围为[0255]。是str
的方法,而不是int
的方法
如果要将服务器响应视为字符串,可以对字节进行解码
string = response.decode('utf-8')
for c in string:
if c.isalpha():
[...]
这消除了该错误,但现在它指出了getCaesar:num+=键中的第10行,错误为:TypeError:unsupported operand type for+=:“int”和“bytes”。客户端程序是否有:message=message.encode()?这消除了该错误,但现在它指出了getCaesar:num+=键中的第10行,错误为:TypeError:不支持+=:“int”和“bytes”的操作数类型。客户端程序是否有:message=message.encode()?