如何在python中使用cURL

时间:2018-04-08 06:44:43

标签: python firebase curl pycurl pyfcm

我想在(python with pycurl)中使用这个cURL命令:

    final String myDate = "08/22/2018 20:56:03";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm/dd/yyyy HH:mm:ss");
    System.out.println("timeeeeeeeeeeeee" + "-" + simpleDateFormat);
    Date date = null;
    try {
        date = simpleDateFormat.parse(myDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    long endtime = date.getTime();
    Date d = null;
    try {
        d = simpleDateFormat.parse(simpleDateFormat.format(new Date(System.currentTimeMillis())));
    } catch (ParseException e2) {
        e2.printStackTrace();
    }
    Long ToCount = endtime -  d.getTime();

    CountDownTimer cdt = new CountDownTimer(ToCount, 1000) {
        public void onTick(long millisUntilFinished) {
            holder.timer.setText(""+ String.format(
                    " %d : %d : %d : %d ",
                    TimeUnit.MILLISECONDS.toDays(millisUntilFinished),
                    TimeUnit.MILLISECONDS.toHours(millisUntilFinished) -
                            TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millisUntilFinished)),
                    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) -
                            TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))
            ));
        }
        public void onFinish() {
            // TODO Auto-generated method stub
            holder.timer.setText("done");
        }
    }.start();

我的源代码是这样的

curl -X POST --header "Authorization: key=AAAAWuduLSU:APA91bEStixxx" --header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d '{"to" : "fC6xEWZwcu0:APA91xxx",  "priority" : "high",  "notification" : {    "body" : "Background Message",    "title" : "BG Title", "sound" : "default"},  "data" : {    "title" : "FG Title",    "message" : "Foreground Message"  }}'

1 个答案:

答案 0 :(得分:0)

当您有请求时,无需在Python中使用curl!

import requests
import json

post_data = {"to" : "fC6xEWZwcu0:APA91xxx",
             "priority" : "high",
             "notification": {"body": "Background Message",
                              "title": "BG Title",
                              "sound": "sound.mp3"},
             "data": {"title": "FG Title",
                      "message": "Foreground Message"}}

header_data = {'Authorization': 'key=AAAAWuduLSU:APA91bEStixxx',
               'Content-Length': '0',
               'Content-type': 'application/json'}

r = requests.post('https://fcm.googleapis.com/fcm/send',
                   data = json.dumps(post_data),
                   headers=header_data)

print('\n\nStatus: %d'.format(r.status_code))
print('TOTAL_TIME: %f'.format(r.elapsed.total_seconds()))
print(r.text)

Haven未对此进行测试,但它应该让您走上正轨。 我还建议您查看Requests module docs以获取更多信息: - )