Ping Baidu and google using Python scripts, pythonping
Ping Service
Ping is an update Notification Service Based on the XML_RPC standard protocol. It is used for Blog to quickly notify the search engine of content updates, so that the search engine can capture and update content in a timely manner.
The computer is equivalent to the RPC Client, which is used to initiate a request to the RPC Server and accept the execution result of the method.
Python Implementation Method
Python has built-in XMLRPClib, which can easily process the XMLRPC protocol, saving the trouble of unpackaging packets.
The usage is very simple. The first import is:
import xmlrpclib
Generate an xmlrpc Server Object:
sever = xmlrpclib.ServerProxy(ServerProxy)
ServerProxy is the RPC server endpoint address of the search engine.
Then you can execute the RPC server method. Take Baidu as an example:
result = server.weblogUpdates.extendedPing(blog_name,index_addr,new_post_addr,rss_addr)
weblogUpdates.extendedPing
Is the method to be executed. The four parameters in the brackets are required on the Baidu ping Service Page. Result is the execution result returned by the method.
Encapsulation code
Inping_all
Put the link to be pinged in the function, and transmit the parameters as needed.
#!/usr/bin/env python# -*- coding:utf-8 -*-import jsonimport xmlrpclibfrom db import redisdef ping(ping_url, *args, **kwds): """args: site_name, site_host, post_url, rss_url.""" rpc_server = xmlrpclib.ServerProxy(ping_url) result = rpc_server.weblogUpdates.extendedPing(*args) print resultdef ping_all(*args, **kwds): ping_url_list = [ 'http://ping.baidu.com/ping/RPC2', 'http://rpc.pingomatic.com/', 'http://blogsearch.google.com/ping/RPC2', ] for url in ping_url_list: ping(url, *args, **kwds)def main(): client = redis.pubsub() client.subscribe(['ping']) while True: for item in client.listen(): if item['type'] == 'message': msg = item['data'] if msg: post = json.loads(msg) print post ping_all(post.get('site_name'), post.get('site_host'), post.get('post_url'), post.get('rss_url'))def test(): site_name = "tech2ipo" site_host = "http://alpha.tech2ipo.com" post_url = 'http://alpha.tech2ipo.com/100855' rss_url = "http://alpha.tech2ipo.com/rss/alpha.tech2ipo.com" ping_all(site_name, site_host, post_url, rss_url)if __name__ == '__main__': main()
Summary
The above is all about this article. I hope this article will help you learn or use python. If you have any questions, please leave a message.