python getostime_Python os.getrandom()用法及代码示例
Python中的OS模塊提供了與操作系統進行交互的功能。操作系統屬于Python的標準實用程序模塊。該模塊提供了使用依賴于操作系統的功能的便攜式方法。
os.getrandom()方法用于生成適合加密使用的大小隨機字節的字符串,或者可以說此方法生成包含隨機字符的字符串。它也可以用來為user-space隨機數生成器提供種子。它可以返回的字節數少于請求的字節數。
用法: os.getrandom(size, flag)
參數:
size:它是字符串隨機字節的大小
flag:它是一個位掩碼,可以包含零個或多個標志或。標志為os.GRND_RANDOM和GRND_NONBLOCK。
返回值:此方法返回一個字符串,該字符串表示適合加密用途的隨機字節。
標志–
os.GRND_NONBLOCK: If this flag is set then getrandom() does not block but instead immediately raises BlockingIOError if no random bytes are available to read.
os.GRND_RANDOM: If this bit is set then random bytes are drawn from the /dev/random pool.
示例1:
# Python program to explain os.getrandom() method
# importing os module
import os
# Declaring size
size = 5
# Using os.getrandom() method
# Using os.GRND_NONBLOCK flag
result = os.getrandom(size, os.GRND_NONBLOCK)
# Print the random bytes string
# Output will be different everytime
print(result)
輸出:
b'5\n\xe0\x98\x15'
示例2:
# Python program to explain os.getrandom() method
# importing os module
import os
# Declaring size
size = 5
# Using os.getrandom() method
# Using os.GRND_RANDOM flag
result = os.getrandom(size, os.GRND_RANDOM)
# Print the random bytes string
# Output will be different everytime
print(result)
輸出:
b'\xce\xc8\xf3\x95%'
總結
以上是生活随笔為你收集整理的python getostime_Python os.getrandom()用法及代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 表结构 增删改查_mysql
- 下一篇: python中升级pip报错_linux