这段PHP代码写成Python 应该怎么写呢?

<?php 
        // Your details
        $token = "abcdefg1234567";
        $email = "abc@gmail.com";
        $timestamp = time();
        
        // Build hash
        $hash = hash('sha256', $token . "|" . $timestamp . "|" . $email);
        
        // Build url
        $myUrl = "http://admin.plugrush.com/api/v2/stats/publisher/dates?hash=" . $hash . "&timestamp=" . $timestamp . "&email=" . $email;
        // create curl resource 
        $ch = curl_init(); 
        // set url 
        curl_setopt($ch, CURLOPT_URL, $myUrl); 
        // return the transfer as a string 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        // $output contains the output string, which is the result of the api request 
        $output = curl_exec($ch); 
        // close curl resource to free up system resources 
        curl_close($ch);      
?>

请问这段代码怎么写成python,一个官方文档的API,无奈不懂PHP。
谢谢。
阅读 2.4k
4 个回答

就是用http发送一个get请求,你用python实现一下就好了。

没有测试,你可以试试这段代码,我用的是python3

import requests
import time
import hashlib

token = 'abcdefg1234567'
email = 'abc@gmail.com'

url = 'http://admin.plugrush.com/api/v2/stats/publisher/dates'

x = hashlib.sha256()
x.update((token + '|' + str(int(time.time())) + '|' + email).encode(encoding='utf-8'))
hash_str = x.hexdigest()
# print(hash_str)
params = {
    'hash': hash_str,
    'timestamp': int(time.time()),
    'email': email
}

resoponse = requests.get(url, params=params)

# 处理结果
print(resoponse.text)
新手上路,请多包涵
import hashlib
import time

import requests

token = "abcdefg1234567"
email = "abc@gmail.com"
timestamp = int(time.time())

hash_str = hashlib.sha256(
    "{}|{}|{}".format(token, timestamp, email).encode("utf-8")
).hexdigest()

url = "http://admin.plugrush.com/api/v2/stats/publisher/dates"

params = {"hash": hash_str, "timestamp": timestamp, "email": email}
resoponse = requests.get(url, params=params)
print(resoponse.json())
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题