How does this PHP or ruby post code translate into Python?

Source: Internet
Author: User
This is a Baidu webmaster platform push Case:

Post case:

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

Examples of PHP pushes:

$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;

Examples of 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

In fact, it should be very simple, that is, how to submit data with Python post.
But look at PHP and Ruby's code is not very similar ~
Want to have a person to point, personal comparison dish ~ Hope to have a detailed translation, thank you!

Reply content:

This is a Baidu webmaster platform push Case:

Post case:

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

Examples of PHP pushes:

$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;

Examples of 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

In fact, it should be very simple, that is, how to submit data with Python post.
But look at PHP and Ruby's code is not very similar ~
Want to have a person to point, personal comparison dish ~ Hope to have a detailed translation, thank you!

#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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.