Python 如何正确转换字符串而不是可变字符串
我正在尝试将我的加密程序合并到聊天程序中。但是我不能用套接字传递可变字符串。那么,我如何正确地将可变字符串转换为正确的字符串呢Python 如何正确转换字符串而不是可变字符串,python,Python,我正在尝试将我的加密程序合并到聊天程序中。但是我不能用套接字传递可变字符串。那么,我如何正确地将可变字符串转换为正确的字符串呢 import sys from UserString import MutableString def GetMode(): while True: print '\n(E)ncrypt Or (D)ecrypt?' Mode = raw_input().lower() if Mode in 'e d encrypt decrypt'.spl
import sys
from UserString import MutableString
def GetMode():
while True:
print '\n(E)ncrypt Or (D)ecrypt?'
Mode = raw_input().lower()
if Mode in 'e d encrypt decrypt'.split():
return Mode
else:
print '\nEnter Proper Choice!'
def GetInput():
while True:
print '\n(T)ype Message Or (L)oad File?'
Input = raw_input().lower()
if Input in 't type'.split():
Input = raw_input('>')
return Input
elif Input in 'l load'.split():
MsgLoc = raw_input()
MsgLoc = open(MsgLoc, 'r')
try:
Input = MsgLoc.read()
MsgLoc.close()
return Input
except:
print '\nCould Not Open' , MsgLoc
else:
print '\nEnter Proper Choice!'
def GetKey():
while True:
Key = 0
print '\nPlease Enter A 20 Digit Number...\n** Do NOT use zeros!!!! EX-NAY ERO-ZAY! **'
try:
Key = int(input())
Key = str(Key)
if (len(Key) == 20):
return Key
else:
print('\nPlease Enter A Valid Number!')
except:
print('\nPlease Enter A Valid Number!')
def Translate(Mode, Input, Key):
if Mode[0] == 'e':
print('\nEncrypting....')
Encrypt(Input, Key)
else:
print('\nDecrypting....')
Decrypt(Input, Key)
def Encrypt(Input, Key):
Msg = MutableString()
NonMutMsg = Input
Msg += NonMutMsg
MsgLen = len(Msg)
CypherKey = Key
a = 0
b = 19
#Loop For Proccessing Key
for z in range(10):
KeySkip = int(CypherKey[a])
KeyIncrement = int(CypherKey[b])
c = MsgLen/KeySkip
d = -1
#Loop To Skip Then Increment
for y in range(c):
d = d+KeySkip
LtrNum = ord(NonMutMsg[d])
LtrNum = LtrNum + KeyIncrement
Msg[d] = chr(LtrNum)
首先,作为缔约国:
应该注意的是,与真正的字符串或Unicode对象相比,这些类效率很低;对于MutableString尤其如此
最重要的是:
这门课的主要目的是作为传承的教育榜样
换句话说,不要在实际代码中使用此类
要获取数据,请使用数据参数:
pythonstring = mutablestring.data
你为什么要使用可变字符串?这门课的主要目的是作为一个教育性的例子,参见文档。此外,通过在每一行之后添加一个不必要的空行来加倍LOC是有害的,而且代码的数量也没有帮助。