Home >Backend Development >PHP Tutorial >How to convert this PHP or Ruby post code into Python?

How to convert this PHP or Ruby post code into Python?

WBOY
WBOYOriginal
2016-07-06 13:52:411378browse

This is a push example from Baidu Webmaster Platform:

post example:

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 push example:

<code class="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;</code>

ruby example

<code class="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</code>

In fact, it should be very simple, that is, how to submit data using python post.
But the codes of php and ruby ​​are not very similar~
I hope someone with experience can give me some advice, I am a better person~I hope there is a detailed translation Thank you very much!

Reply content:

This is a push example from Baidu Webmaster Platform:

post example:

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 push example:

<code class="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;</code>

ruby example

<code class="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</code>

In fact, it should be very simple, that is, how to submit data using python post.
But the codes of php and ruby ​​are not very similar~
I hope someone with experience can give me some advice, I am a better person~I hope there is a detailed translation Thank you very much!

<code>#coding=utf-8

import requests

urls = [
    '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</code>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn