【发布时间】:2016-04-07 09:05:55
【问题描述】:
我在 python 中创建了一个 HTTP 服务器,它执行一些加密算法。因此,我尝试在 GET 方法之外为我的密码学创建重要变量,因为我不希望它们在每次请求后都发生变化。
下面是代码示例:
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
path = self.path
print 'request for ' + path
rootdir = '/Server'
client_mac = path.split('?')[-1]
file_name = path.split('?')[0]
if self.path.split('?')[0].endswith('.txt'):
'''create client ID'''
print 'Printing mac address of connected device ----> %s' % client_mac
id1_secret_key = pre.keyGen(master_secret_key, client_mac)
fr = open (rootdir + file_name)
file_data = fr.read()
sym_key_ciphertext = pre.encrypt(params, client_mac, sym_key);
#more code follows
try:
httpd = HTTPServer(('',8080), Handler)
print 'Server is on and listening to port...7000'
group = PairingGroup('SS512', secparam=1024)
pre = PreGA(group)
(master_secret_key, params) = pre.setup()
print params
sym_key = OpenSSLRand().getRandomBytes(128) #128 bits
sym_cipher = AuthenticatedCryptoAbstraction(sym_key)
server_mac = get_mac()
id2_secret_key = pre.keyGen(master_secret_key, str(server_mac))
httpd.serve_forever()
except KeyboardInterrupt:
print '^C received,shutting down the web server'
server.socket.close()
执行 GET 请求后,我收到以下错误:
File "server.py", line 157, in do_GET
ciphertext = pre.encrypt(params, client_mac, file_data);
UnboundLocalError: local variable 'params' referenced before assignment
我应该提到变量 params 在 pre.setup() 执行后有一个值。任何建议都会有所帮助并深表感谢。
【问题讨论】:
-
您的代码不清楚。
try部分是在功能内部还是外部?课内还是课外?请相应地修正缩进 -
尝试在类外