简体   繁体   English

转换bash代码以将sh1转换为python代码以执行相同的操作

[英]convert bash code to generate sh1 into python code to do the same

i'm using the following code to generate sh1 via bash 我正在使用以下代码通过bash生成sh1

gen_sh1="api_format=$api_format&api_key=$api_key&api_nonce=$api_nonce&api_timestamp=$api_timestamp&custom.videoId="$customvideoId"&description=$description&downloadurl=$sourceurl&duration=$duration&sourceformat=$sourceformat&sourcetype=$sourcetype&sourceurl=$sourceurl&title="$description"GKxS7otlAsujiRxXHTvshUE9"

then 然后

api_signature=`echo -n $gen_sh1 | sha1sum | awk '{print $1}'`

to catch the value , 抓住价值,

what is the equivalent in python to generate the same sh1 with parameters i'm passing and catch the value 在python中用我传递的参数生成相同的sh1并捕获值的等效条件是什么

please advise 请指教

Just import the hashlib module that is included with Python, and do something like this: 只需导入Python随附的hashlib模块,然后执行以下操作:

import hashlib


# Assuming all arguments to format have been defined above...
gen_sh1 = "api_format={}&api_key={}&api_nonce={}&api_timestamp={}&custom.videoId={}&description={}&downloadurl={}&duration={}&sourceformat={}&sourcetype={}&sourceurl={}&title={}GKxS7otlAsujiRxXHTvshUE9".format(
    api_format,
    api_key, 
    api_nonce,
    api_timestamp,
    customvideoId,
    description,
    sourceurl,
    duration,
    source_format,
    sourcetype,
    sourceurl, 
    description) 

api_signature = hashlib.sha1(gen_sh1).hexdigest()

print api_signature

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM