def writeConfiguredCookieFile(self, cookie_string = None):
'''
Write a random 32-byte value to the configured cookie file.
If cookie_string is not None, use that value.
Return the value written to the file, or None if there is no cookie
file, or if writing the file fails.
'''
cookie_file = self.getConfiguredCookieFile()
if cookie_file is not None:
if cookie_string is None:
cookie_string = urandom(TorControlProtocol.SAFECOOKIE_LENGTH)
try:
with open(cookie_file, 'w') as f:
f.write(cookie_string)
except IOError as e:
logging.warning("Disabling SAFECOOKIE authentication, writing cookie file '{}' failed with error: {}"
.format(cookie_file, e))
return None
# sanity check: this will fail in write-only environments
assert cookie_string == TorControlProtocol.readCookieFile(
cookie_file)
return cookie_string
else:
return None
评论列表
文章目录