python 随机生成UUID
时间: 2024-01-05 18:21:02 浏览: 170
UUID随机数字
以下是两种Python随机生成UUID的例子:
1. 使用uuid模块生成随机UUID:
```python
import uuid
def get_uuid():
return str(uuid.uuid4())
# 调用get_uuid()函数生成随机UUID
random_uuid = get_uuid()
print(random_uuid)
```
2. 使用自定义的UUID生成器类生成不同类型的UUID:
```python
import uuid
class UUIDGenerator:
def get_timestamp_uuid(self):
return str(uuid.uuid1())
def get_randomnumber_uuid(self):
return str(uuid.uuid4())
def get_specifiedstr_uuid(self, namespace, name):
return str(uuid.uuid3(namespace, name))
def get_specifiedstr_SHA1_uuid(self, namespace, name):
return str(uuid.uuid5(namespace, name))
# 实例化UUIDGenerator类
uuid_generator = UUIDGenerator()
# 调用不同方法生成不同类型的UUID
print(uuid_generator.get_timestamp_uuid())
print(uuid_generator.get_randomnumber_uuid())
print(uuid_generator.get_specifiedstr_uuid(uuid.NAMESPACE_DNS, 'example.com'))
print(uuid_generator.get_specifiedstr_SHA1_uuid(uuid.NAMESPACE_DNS, 'example.com'))
```
阅读全文