Python生成唯一id的方法

 1. uuid

import uuid

def create_uid():
    return str(uuid.uuid1())

if __name__ == '__main__':
    print(type(create_uid()))
    print(create_uid())
    print(create_uid())
    print(create_uid())

---
<class 'str'>
cdcb9030-51e8-11e7-881d-3c52824c8d6f
cdcb9031-51e8-11e7-86b4-3c52824c8d6f
cdcb9032-51e8-11e7-be6c-3c52824c8d6f

2. hashlib+time

  - 示例1:

import time,hashlib

def create_id():
    m = hashlib.md5(str(time.clock()).encode('utf-8'))
    return m.hexdigest()
if __name__ == '__main__':
    print(type(create_id()))
    print(create_id())
    print(create_id())
    print(create_id())

---
<class 'str'>
b96d5fca757c00cbd53ae672f60e7ffe
3e8391327caa4850077768d88ee18c7e
df28a1e9895052608c2a6897f13d5659

  - 示例2:

import time,hashlib

def create_id():
    m = hashlib.md5()
    m.update(bytes(str(time.clock()),encoding='utf-8'))
    return m.hexdigest()
if __name__ == '__main__':
    print(type(create_id()))
    print(create_id())
    print(create_id())
    print(create_id())

---
<class 'str'>
be4a24bec22f46c84a4dde78e68cb838
53e8e0c48c885b91fb2565cbe87b8423
f8fd24c3b8ce1a39217134e22203a5c4

3. scrapy.utils.request.request_fingerprint

参考:http://www.cnblogs.com/standby/p/7846478.html

 

posted @ 2017-06-16 00:43  lixin[at]hitwh  阅读(12317)  评论(2编辑  收藏  举报