ruby,python及curl post请求

#飘红部分为变量

test_url="http://test"

body_hash={"value"=>100, "year"=>2014, "month"=>11, "day"=>12, "hour"=>16, "minute"=>9, "second"=>0, "host"=>"test"}

body_json=body_hash.to_json

#for curl

curl -X POST -H "Content-Type: application/json" -d $body_json $test_url

#for ruby

def send_data(test_url,body_hash)

data=body_hash

data=data.to_json

puts "data for json is #{data}"

url = URI.parse(test_url)

req = Net::HTTP::Post.new(url.path,{‘Content-Type‘ => ‘application/json‘})

req.body = data

begin

res = Net::HTTP.new(url.host,url.port).start{|http| http.request(req)}

rescue Exception

res = ‘‘

puts "post to dest failed!"

return 1
    end

post_res=res.body

puts "post result is #{post_res}"

end

=================================================

#sms发送短信:

#网关给的api请求key,使用 HTTP Basic Auth 方式进行身份验证,使用api作为验证用户名,API key是验证密码

api-key=key-14a206a26676b946e8df722axxxxxxxx

#网关请求api地址

api-url="https://sms-api.xxx.com/v1/send.json"

#短信签名,短信签名指附加在短信内容末尾的署名信息,使用发送者的公司名或产品名,格式为:【XXXXX】

api-name="【测试】"

#for curl

curl -s -k --user api:$api-key $api-url -F mobile=‘12345678901‘ -F message=‘Send message for test! $api-name‘

#for python

mob="12345678901"

msg="for test"+api-name

def send_messag(mob,msg):
    print mob
    print msg
    resp = requests.post((api-url),
    auth=("api", api-key),
    data={
    "mobile": mob,
    "message": msg
    },timeout=3 , verify=False);
    result = json.loads( resp.content )
    print result

#for php,未测试

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$api-url);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);

curl_setopt($ch, CURLOPT_USERPWD , ‘api:$api-key‘);

curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_POSTFIELDS, array(‘mobile‘ => ‘12345678901‘,‘message‘ => ‘test【测试】‘));

$res = curl_exec( $ch );

curl_close( $ch );

//$res = curl_error( $ch );

var_dump($res);

				
时间: 2024-08-19 13:07:22

ruby,python及curl post请求的相关文章

用 python 或者curl 抓取web数据时

今天接到一个web的同事求助,他本来通过java抓取一个web页面的内容,但是发现抓取得都是些乱码,然后又尝试用 python的urllib来获取,依然是乱码,不过在浏览器访问却是正常的json格式数据. 首先,我先用curl获取了下这个web,发现确实是一些不可见的字符,把它保存下来. 再使用浏览器获取下这个web内容,对比下发现字节数是不一样的,说明不是编码显示的问题. 这个时候我考虑可能是http协议参数的问题,于是使用wireshark捕获用浏览器的请求,用tcpdump捕获了curl的

python get post模拟请求

1.使用get方式时,url类似如下格式: [html] view plaincopy index.jsp?id=100&op=bind GET报问头如下: [html] view plaincopy GET /sn/index.php?sn=123&n=asa HTTP/1.1 Accept: */* Accept-Language: zh-cn host: localhost Content-Type: application/x-www-form-urlencoded Content

使用PHP中的curl发送请求

使用CURL发送请求的基本流程 使用CURL的PHP扩展完成一个HTTP请求的发送一般有以下几个步骤: 初始化连接句柄: 设置CURL选项: 执行并获取结果: 释放VURL连接句柄. 下面的程序片段是使用CURL发送HTTP的典型过程 // 1. 初始化 $ch = curl_init(); // 2. 设置选项,包括URL curl_setopt($ch,CURLOPT_URL,"http://www.devdo.net"); curl_setopt($ch,CURLOPT_RETU

Python 和curl 调用sendcloud发送邮件

python代码示例: https://github.com/sendcloud2013/sendcloud_docs/blob/master/sources/downloads/code/python.md curl示例: https://github.com/sendcloud2013/sendcloud_docs/blob/master/sources/downloads/code/curl.md

Python发一个GET请求

# -*- coding: utf-8 -*- try: import httplib2 except ImportError: print('错误:') print(' httplib2这个库没有找到,程序无法继续执行!') exit(255) def network_get_proc(self, use_cache = True): '''POST动作''' get_request_str = 'http://192.168.0.54/softlist' # 请求URL # print('-

python error: curl: (1) Protocol "'https" not supported or disabled in libcurl

python 调用curl访问一个网页时,出现error: curl: (1) Protocol "'https" not supported or disabled in libcurl 控制台直接curl xxx是ok的 output = subprocess.check_output(["curl","https://stackoverflow.com/questions/6884669/curl-1-protocol-https-not-suppo

转:PHP中的使用curl发送请求(GET请求和POST请求)

原文地址:http://www.jb51.net/article/104974.htm 使用CURL发送请求的基本流程 使用CURL的PHP扩展完成一个HTTP请求的发送一般有以下几个步骤: 1.初始化连接句柄: 2.设置CURL选项: 3.执行并获取结果: 4.释放VURL连接句柄. 下面的程序片段是使用CURL发送HTTP的典型过程: // 1. 初始化 $ch = curl_init(); // 2. 设置选项,包括URL curl_setopt($ch,CURLOPT_URL,"http

curl get请求添加header头信息

function get($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPGET, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出. $header = ['User-Agent: php test']; //设置一个你的浏览器agent的header curl_setopt($ch, CURLOPT

shell vs python脚本监控http请求

各写一个shell和python脚本来监控http请求,并在服务不可用的时候重启服务. 监控的连接为: http://192.168.1.101:5022/product http://192.168.1.101:5024/module shell脚本如下,配合crontab计划任务每一分钟执行一次检查: #!/bin/bash # This shell is used to moniter 192.168.1.101 port 5022 & 5024 date  #在crontab里用来记录l