這段php或是ruby的post代碼如何轉換翻譯成Python?

來源:互聯網
上載者:User
這是一個百度站長平台的推送案例:

post案例:

POST /urls?site=www.nantongzt.com&token=xxxxxx HTTP/1.1
User-Agent: curl/7.12.1
Host: data.zz.baidu.com
Content-Type: text/plain
Content-Length: 83
http://www.example.com/1.html
http://www.example.com/2.html

php的推送案例:

$urls = array(    'http://www.example.com/1.html',    'http://www.example.com/2.html',);$api = 'http://data.zz.baidu.com/urls?site=www.nantongzt.com&token=xxxxxx';$ch = curl_init();$options =  array(    CURLOPT_URL => $api,    CURLOPT_POST => true,    CURLOPT_RETURNTRANSFER => true,    CURLOPT_POSTFIELDS => implode("\n", $urls),    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),);curl_setopt_array($ch, $options);$result = curl_exec($ch);echo $result;

ruby的案例

require 'net/http'urls = ['http://www.example.com/1.html', 'http://www.example.com/2.html']uri = URI.parse('http://data.zz.baidu.com/urls?site=www.nantongzt.com&token=xxxxxx')req = Net::HTTP::Post.new(uri.request_uri)req.body = urls.join("\n")req.content_type = 'text/plain'res = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }puts res.body

其實應該很簡單,就是如何用python post提交資料.
但是看php和ruby的代碼又不是很像~
希望有高人指點一下,個人比較菜~希望有翻譯得詳細一點,謝謝了!

回複內容:

這是一個百度站長平台的推送案例:

post案例:

POST /urls?site=www.nantongzt.com&token=xxxxxx HTTP/1.1
User-Agent: curl/7.12.1
Host: data.zz.baidu.com
Content-Type: text/plain
Content-Length: 83
http://www.example.com/1.html
http://www.example.com/2.html

php的推送案例:

$urls = array(    'http://www.example.com/1.html',    'http://www.example.com/2.html',);$api = 'http://data.zz.baidu.com/urls?site=www.nantongzt.com&token=xxxxxx';$ch = curl_init();$options =  array(    CURLOPT_URL => $api,    CURLOPT_POST => true,    CURLOPT_RETURNTRANSFER => true,    CURLOPT_POSTFIELDS => implode("\n", $urls),    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),);curl_setopt_array($ch, $options);$result = curl_exec($ch);echo $result;

ruby的案例

require 'net/http'urls = ['http://www.example.com/1.html', 'http://www.example.com/2.html']uri = URI.parse('http://data.zz.baidu.com/urls?site=www.nantongzt.com&token=xxxxxx')req = Net::HTTP::Post.new(uri.request_uri)req.body = urls.join("\n")req.content_type = 'text/plain'res = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }puts res.body

其實應該很簡單,就是如何用python post提交資料.
但是看php和ruby的代碼又不是很像~
希望有高人指點一下,個人比較菜~希望有翻譯得詳細一點,謝謝了!

#coding=utf-8import requestsurls = [    'http://www.example.com/1.html',    'http://www.example.com/2.html']api = 'http://data.zz.baidu.com/urls?site=www.nantongzt.com&token=xxxxxx'data = '\n'.join(urls)headers = {'Content-Type': 'text/plain'}r = requests.post(api, data=data, headers=headers)print r.text

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.