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" --connect-timeout 10 -m 20 -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 do |http|
http.read_timeout=5
http.request(req)
end
rescue Exception
res = ''
puts "error:#{$!} at:#{$@}!"
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);
================ruby uri====================
require 'uri'
uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413")
#=> #<URI::HTTP:0x00000000b14880
URL:http://foo.com/posts?id=30&limit=5#time=1305298413>
uri.scheme
#=> "http"
uri.host
#=> "foo.com"
uri.path
#=> "/posts"
uri.port
#=> "80"
uri.query
#=> "id=30&limit=5" uri.fragment #=> "time=1305298413"
uri.to_s
#=> "http://foo.com/posts?id=30&limit=5#time=1305298413"
ruby,python及curl post请求的更多相关文章
- 【转】提交http请求之python与curl
提交http请求之python与curl 由于Openstack是python实现wsgi的REST ful架构,在学习和调试的过程中,常常会遇到http请求的提交,于是顺手整理下python和cur ...
- python学习--curl
PyCurl是一个C语言写的libcurl的python绑定库.libcurl 是一个自由的,并且容易使用的用在客户端的 URL 传输库.它的功能很强大,PycURL 是一个非常快速(参考多并发操作) ...
- python 3 处理HTTP 请求的包
http http: https://docs.python.org/3/library/http.html http是一个包,里面含有多个模块:http.client,http.server,htt ...
- python CSRF跨站请求伪造
python CSRF跨站请求伪造 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- curl get请求添加header头信息
function get($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPGET, true); curl_setopt($ch, CU ...
- linux c++ curl https 请求并双向验证SSL证书
1.配置curl https请求需要提供 CA证书.客户端证书和客户端秘钥,这三个文件的pem格式. 分别对应 curl_easy_setopt() 函数的 下面三个参数: CURLOPT_CAINF ...
- python发送HTTP POST请求
1. 127.0.0.1和0.0.0.0 127.0.0.1是一个回送地址,指本地机,一般用来本机测试使用,使用127.0.0.1启的服务只能在本地机器上访问,使用0.0.0.0启的服务可以在其他机器 ...
- 转:PHP中的使用curl发送请求(GET请求和POST请求)
原文地址:http://www.jb51.net/article/104974.htm 使用CURL发送请求的基本流程 使用CURL的PHP扩展完成一个HTTP请求的发送一般有以下几个步骤: 1.初始 ...
- python error: curl: (1) Protocol "'https" not supported or disabled in libcurl
python 调用curl访问一个网页时,出现error: curl: (1) Protocol "'https" not supported or disabled in lib ...
随机推荐
- Python面向对象编程 - 一个记事本程序范例(一)
notebook.py import datetime last_id = 0 class Note: '''Represent a note in the notebook. Match again ...
- web开发学习之旅
过段时间要去实习,提前问了下老师我要准备哪些知识. 2015年3月19日,老师告诉我的,ionic Framework,Yii Framework,AngularJS,还有一些前端开发知识. 我除了听 ...
- 【oracle】dblink创建
目的:oracle中跨数据库查询 两台数据库服务器db_A(本地)和db_B(远程192.168.1.100),db_A下用户user_a 需要访问到db_B下user_b的数据 解决:查询得知使用d ...
- 别人的spring学习入门笔记
http://elf8848.iteye.com/blog/875830 可以做入门参考,英语好可以阅读spring的spring-framework-reference 更多学习blog http ...
- repr
>>> import datetime >>> today=datetime.datetime.now() >>> today datetime. ...
- spring MVC之构造ModelAndView对象
spring MVC之构造ModelAndView对象 ---------- 构造ModelAndView对象 当控制器处理完请求时,通常会将包含视图名称或视图对象以及一些模型属性的ModelAndV ...
- 【TP3.2】:日志记录和查看
1.TP3.2手册日志类链接:http://document.thinkphp.cn/manual_3_2.html#log 2.日志默认路径:/Application/Runtime/Logs 3. ...
- zabbix监控redis连接情况
配置zabbix客户端配置文件 vim /etc/zabbix/zabbix_agentd.conf 添加 Include=/etc/zabbix/zabbix_agentd.d/ 添加脚本对red ...
- invalid configuration x86_64-unknown-linux-gnu' machine x86_64-unknown' not recognized
转载自:http://blog.csdn.net/php_boy/article/details/7382998 前两天在装机器软件的时候, 出现了下面的错误, invalid configurati ...
- windows添加开机启动项
http://www.cnblogs.com/jokey/archive/2010/06/17/1759370.html添加开机启动项(通过注册表) 例子:增加QQ开机启动项 第一步:找到注册表的启动 ...