Python communicates with PHP via XMLRPC

Source: Internet
Author: User
Tags php language

Python communicates with PHP via XMLRPC: Server-side python, PHP for clients.

Server side: xmlrpc_server.py

#!/usr/bin/python#Coding:utf-8 fromSimplexmlrpcserverImportSimplexmlrpcserver fromSocketserverImportThreadingMixInclassThreadxmlrpcserver (ThreadingMixIn, Simplexmlrpcserver):Pass#define your own CMS classclassMycms:    defechostring (Self, STRs):s = u'From Python-'        returnS +STRsdefSayHello (self):return "Hello XMLPRC"#Create an instanceCMS =mycms () server= Threadxmlrpcserver (("localhost", 8888), allow_none=True)#Registerserver.register_instance (CMS)#the server executes and listens on this portPrint "Listening on port 8888"Server.serve_forever ()

Client: xmlrpc_client.php

<?PHP//XML-RPC Client Demo program///require (' libphpxmlrpc/xmlrpc.inc ');include(' Libphpxmlrpc/xmlrpc.inc ');$xmlrpc _internalencoding= ' UTF-8 ';//Create a client object, three parameters in turn path, hostname, Port$client=NewXmlrpc_client ('/', ' localhost ', 8888);//create Xmlrpcval object, which allows the encoding of our variable//creates Xmlrpcval objects, encodes our PHP variable into the XML form required by Xml-rpc$inputString= Php_xmlrpc_encode (' Hello world ');//create an array of parameters//although we only have one parameter, we still want to convert the form of the array, because the second parameter of xmlrpcmsg is a parameter table$parameters=Array($inputString);//Create the message object//creates a XML-RPC message with parameters for the remote method name and the parameter table$msg=NewXmlrpcmsg (' echostring ',$parameters);///$msg =new xmlrpcmsg (' SayHello ');//$client->request_charset_encoding = ' utf-8 ';//send the message, get the response//sends a paper, the return value $RSP is a Xmlrpcresp object, it contains the following three methods://FaultCode () error code, if successful will return 0//faultstring () error message//value () return value, exists as a Xmlrpcval object and needs to be decoded before PHP is used$client->debug =true;$RSP=$client->send ($msg);//Check for errorsif($RSP->faultcode () ==0) {    //decode the response to a PHP type//xmlrpc_decode () function is used to decode the Xmlrpcval object    $response=php_xmlrpc_decode ($RSP-value ()); //Print Results    Print' <pre> '; Var_dump($response); ///echo $response;     Print' </pre> ';} Else {    //Print Errors    Print' Error: '.$RSP->faultcode (). ', '.$RSP->faultstring (). ' <br> ';}//Show messages//Check the contents of the message/*$msg->createpayload ();p rint ' request:<xmp> '. $msg->payload. ' </xmp> ';p rint ' response:<xmp> '. $RSP->serialize (). ' </xmp> ';*/?>

Where the client needs the PHP language's XMLRPC Development library.

: http://download.csdn.net/detail/lsmallstop/7741211

After downloading, the Lib directory is copied and named Libphpxmlrpc, and placed in the same directory as the PHP file above.

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.