最近在写python的工具, 总结一下.

  1. type(变量名字) # 可以打印变量类型,以方便处理

  2. len(var)#可获取类型长度

  3. binascii.a2b_hex(string) # 将十六进制数字字符串转换为二进制数据。该函数也称为unhexlify(string)

  4. binascii.b2a_hex(string)
    python 字符串与16进制互转

  5. python 打印对象的所有属性值的方法.

    def prn_obj(obj): print '\n'.join(['%s:%s' % item for item in obj.__dict__.items()])
    

    以上是打印某个对象的所有属性值的方法

  6. python 获取对象信息

  7. pdb 调试
    [python调试方法] (https://www.cnblogs.com/skyus/p/7210234.html)

  8. plain_text.rstrip('\0') #删除字符串末尾0字符

  9. 编译protocol Buffers
    Now that you have a .proto, the next thing you need to do is generate the classes you’ll need to read and write AddressBook (and hence Person and PhoneNumber) messages. To do this, you need to run the protocol buffer compiler protoc on your .proto:
    If you haven’t installed the compiler, download the package and follow the instructions in the README.
    Now run the compiler, specifying the source directory (where your application’s source code lives – the current directory is used if you don’t provide a value), the destination directory (where you want the generated code to go; often the same as $SRC_DIR), and the path to your .proto. In this case, you…:
    protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto
    Because you want Python classes, you use the --python_out option – similar options are provided for other supported languages.
    This generates addressbook_pb2.py in your specified destination directory.

  10. hashlib.sha256(data) # 计算data的哈希值

    >>> import hashlib
    >>> a = hashlib.sha256('aaaa')
    >>> a.update('bbbb')  #将新的数据和旧的数据一起计算哈希值
    >>> a.digest()
    '\xe5\xc1\xed\xb5\x0f\xf8\xb4\xfc\xc3\xea\xd3\xa8E\xff\xbe\x1a\xd5\x1c\x9d\xae]D3Z\\3;W\xac\x8d\xf0b'
    >>> b = hashlib.sha256('aaaabbbb') #相同数据对比update的计算哈希值是相同的
    >>> b.digest()
    '\xe5\xc1\xed\xb5\x0f\xf8\xb4\xfc\xc3\xea\xd3\xa8E\xff\xbe\x1a\xd5\x1c\x9d\xae]D3Z\\3;W\xac\x8d\xf0b'
    >>> b.hexdigest()  # 获取十六进制字符串
    'e5c1edb50ff8b4fcc3ead3a845ffbe1ad51c9dae5d44335a5c333b57ac8df062'
    

    python hashlib模块

  11. logging 日志级别,网上例子很多

  12. python struct模块
    解决字节序问题,字节对齐,

    self.version = struct.unpack('>Q', payload_file.read(8))[0]
    self.manifest_len = struct.unpack('>Q', payload_file.read(8))[0]
    self.metadata_signature_len = struct.unpack('>I', payload_file.read(4))[0]
    

    Python之struct简介
    浅析Python中的struct模块

  13. 用到’openssl’ 这个比较复杂

     def _CheckSha256Signature(sig_data, cert_file_name, actual_hash, sig_name):if len(sig_data) != 256:logger.info('%s: signature size (%d) not as expected (256).' % sig_name, len(sig_data))signed_data, err = RunCommand(['openssl', 'rsautl', '-verify', '-certin', '-inkey', cert_file_name ], send_data=sig_data)if len(signed_data) != len(SIG_ASN1_HEADER) + 32:logger.info('%s: unexpected signed data length (%d).', sig_name, len(signed_data))if not signed_data.startswith(SIG_ASN1_HEADER):logger.info('%s: not containing standard ASN.1 prefix.', sig_name)signed_hash = signed_data[len(SIG_ASN1_HEADER):]if signed_hash != actual_hash:logger.info('%s: signed hash (%s) different from actual (%s).', sig_name, signed_hash.encode('base64').strip(),
    

    直接输入
    openssl x509 -in ca.crt -pubkey
    的确会看到两个公钥输出,
    如果将它转换成der格式文件的公钥文件,则没有任何多余输出
    openssl x509 -in ca.crt -inform PEM -out ca.der -outform DER

    提取公钥
    openssl x509 -in out/otacert -pubkey -noout > pubkey.pem
    /使用公钥进行验证/
    $ openssl rsautl -verify -in sign1.txt -inkey pub.pem -pubin -out replain1.txt
    /使用私钥进行签名/
    $ openssl rsautl -sign -in plain.txt -inkey pri.pem -out sign1.txt
    公钥加密
    openssl rsautl -encrypt -inkey publickey.pem -pubin -in key.bin -out key.bin.en
    将kpcs8 转换到.pem 私钥
    openssl pkcs8 -inform DER -nocrypt -in testkey.pk8 -out testkey.pem
    直接使用证书进行验签,实际也是使用公钥
    openssl rsautl -verify -certin -inkey out/otacert -in old_data -out replain.txt
    使用 openssl 生成证书(含openssl详解)
    https://blog.csdn.net/gengxiaoming7/article/details/78505107

  14. AES128 对称加密
    AES加密算法的详细介绍与实现

  15. subprocess模块

    def RunCommand(command, send_data = None):logger.info('Running command, please wait....')logger.debug('command: %s', command)child = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)try:result, err = child.communicate(input = send_data)#利用管道输入数据. 和返回数据finally:exit_code = child.wait()if exit_code:raise RuntimeError('Subprocess %r failed with code %r.' %(command, exit_code))return result, exit_code
    
  16. import argparse 模块

    import argparse
    parser=argparse.ArgumentParser()
    parser.add_argument("echo",help="echo the string")
    args=parser.parse_args()
    print args.echo
    

    Python命令行解析argparse常用语法使用简介

  17. 填充字符串

    from binascii import b2a_hex, a2b_hex
    h = 'AAA'
    print b2a_hex(he.ljust(16, '\000')) #output: 41414100000000000000000000000000
    

(注意): 这里的'\000'是转义字符, ascii中代表NUL, 和字符’\0’, 是两个东西. 下面是部分ASCII表

Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex
0 00 NUL 16 10 DLE 32 20 空格 48 30 0 64 40 @ 80 50 P 96 60 ` 112 70 p
1 01 SOH 17 11 DC1 33 21 ! 49 31 1 65 41 A 81 51 Q 97 61 a 113 71 q
2 02 STX 18 12 DC2 34 22 " 50 32 2 66 42 B 82 52 R 98 62 b 114 72 r

python项目总结相关推荐

  1. 引入yml依赖包_手把手教你发布 Python 项目开源包

    编译:机器之心,作者:Gabriel Lerner.Nathan Toubiana 好不容易码了个 python 项目,是不是很兴奋?那么怎么把这个项目发出去让大家看到呢?本文作者写了一份在 GitH ...

  2. python项目了解_神级程序员都是这样来开源 Python 项目!今天算是涨知识了!

    工具和概念 项目布局 当准备一个项目时,正确合理的布局(目录结构)是十分重要的.一个合理的布局意味着想参与开发者不必花时间来寻找某些代码的位置; 凭直觉就可以找到文件的位置.因为我们在处理一个项目,就 ...

  3. Python训练营2021:构建8个真实世界的Python项目

    时长:19h 27m |视频:. MP4,1280×720 30 fps |音频:AAC,44.1 kHz,2ch |大小:9.54 GB 语言:英语+中英文字幕 机译 从Python Web开发的初 ...

  4. Github 年度最受欢迎的 TOP30 Python 项目,超值

    作者 | 俊欣 来源 | 关于数据分析与可视化 今天小编整理归纳了2021年Github上面最受欢迎的30个Python项目,帮助大家在打磨技术与提升自我上面更进一步. 通过代码来获取 Github官 ...

  5. 70+Python项目,面向初学者、中级和经验丰富的开发人员

    建立动手项目将帮助您获得实用的编码技能.一步一步,你将把你的理论知识运用和建立一个令人印象深刻的投资组合.如果您是一名经验丰富的Python开发人员,您可能已经听说并搜索了以下问题:"对于初 ...

  6. 有趣的python项目_推荐个超好玩的Python项目

    原标题:推荐个超好玩的Python项目 来自:Python之禅(微信号:VTtalk) GitHub上有个很有意思的项目,这个项目就是将一张图片转换成一个网页,例如这张蒙娜丽莎的微笑转换成网页之后的效 ...

  7. 完整的python项目流程

    最近看了python项目的打包,有点小感触.感觉自己还不是一个真正的pythoner.没有在开源项目网站留下什么,而又到处宣扬python的简洁与强大,有点小讽刺.于是乎,我想理清自己的思路,不怕迷茫 ...

  8. python项目中requirements的巧用(一键导入所有安装包)

    一个Python 项目中可能安装很多安装包, 再次创建虚拟环境是需要重新安装的话很麻烦也费时间, 或者项目部署的时候避免重装, 可以将现有项目的所有安装包记录在requirements.txt 文件, ...

  9. python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程

    python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程 参考文章: (1)python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程 ...

  10. python项目打包

    原文:https://packaging.python.org/tutorials/packaging-projects/ 本教程将指导您如何打包一个简单的Python项目.它将向您展示如何添加必要的 ...

最新文章

  1. bn层Expected more than 1 value per channel when training, got input size torch.Size
  2. JDK1.7配置及测试
  3. 7-2 整除分块 (15 分)
  4. 看FusionInsight Spark如何支持JDBCServer的多实例特性
  5. 【Jenkins】Jenkins配置从节点,实现远程主机调用功能
  6. 组内Linq培训记录
  7. BXP无盘XP2000系统安装详解
  8. LoadRunner教程01:性能测试常见用语
  9. LCN(使用springCloud)分布式事物原理
  10. 三明学院信息工程学院网络攻防大赛-初赛官方解题报告
  11. 怎样激火一个儒雅随和的程序员呢
  12. 操作系统实验读者写者程序源码_我的操作系统梦破灭了
  13. 【appium】appium自动化入门之基本参数完整版(67个appium基本参数和关键字)
  14. ae中计算机打字预设,Typewriter Pro(AE电脑打字动画特效预设)
  15. 入行大数据需要学习哪些编程语言
  16. CDA LEVELⅠ2021新版模拟题二(附答案)
  17. C#短网址压缩算法与短网址原理入门
  18. 图灵奖得主Yoshua Bengio:用因果打开AI的黑盒
  19. php做网站需要html,杭州做网站:所有新的PHP, CSS和HTML帮助表
  20. 微信小程序实现图案绘制

热门文章

  1. 网站2008服务器32位好还是64位好,win server 2008 32位与64位区别
  2. 解决双启动GRLDR missing故障的方法
  3. 武汉大学计算机 韩立,文澜学术系列讲座 第135期 武汉大学经济与管理学院韩立宁老师:“Financial Network and Industry Connectedness”...
  4. 提问的智慧/ 如何优雅的提问
  5. 数据库学习-关于存在量词exists的理解
  6. IPv6技术精要--第14-16章 IPv6路由选择
  7. 16.火焰传感器实验
  8. 怎样把IE设置成默认浏览器
  9. shel脚本中批量替换文件名
  10. Kali Linux pyqt5 运行报错xcb