从python中的curl命令获取输出。

时间:2018-07-11 15:09:48

标签: python curl

如何将curl命令的结果设置为一个变量,(因此在此ip地址中。)

import subprocess; ip = subprocess.call("curl ident.me", shell=True)
120.12.12.12

ip returns "0" 

1 个答案:

答案 0 :(得分:2)

与通过外壳运行curl相比,使用>>> import requests >>> r = requests.get('http://ident.me') >>> r.text '137.221.143.78' >>> 模块效果更好

requests

如果由于某种原因您不能使用subprocess.check_output模块,然后尝试使用>>> ip = subprocess.check_output("curl -s ident.me".split()) >>> ip '137.221.143.78'

            // For each room grab the pins.
            foreach($rooms as $room) {

                // Grab all inspiration_id's for a specific room.
                $pins = Pins::where('room_id', $room['id'])->get();

                // Inspirations array.
                $inspirations = array();


             // For each pin use inspiration_id to grab inspiration id.
                foreach($pins as $pin) {

                    // Grab inspiration that matches the pin.
                    $pin = Inspirations::where('id', $pin['inspiration_id'])->get();

                    // Add a $pin to the array.
                    array_push($inspirations, $pin);

                }

                // Input inspirations into pins of this room.
                $room['pins'] = $inspirations;

            }