PHP or Python calls the server implemented by C ++

Source: Internet
Author: User
Two days on the weekend, I wrote a php_cerl module one day to complete the phpclient of cerl, and a pycerl module one day to complete the pythonclient of cerl. The C ++ server can be called using php or python. For the HashServer we previously wrote with CERL (for a very simple example, see Erlangvs.

Two days on the weekend, I wrote a php_cerl module one day to complete the php client of cerl. I wrote a pycerl module one day to complete the python client of cerl. The C ++ server can be called using php or python.

For the HashServer we previously wrote with CERL (for a very simple example, see Erlang vs. CERL-CERL introduction), the PHP Client sample is as follows:

View plaincopy to clipboardprint?
Define ('hashsever _ put', 1 );
Define ('hashsever _ get', 2 );
 
If (! Extension_loaded ('php _ cerl ')){
If (! Dl ('php _ cerl. '. PHP_SHLIB_SUFFIX ))
Die ("ERROR: Cannot load php_cerl library! \ N ");
}
 
Cerl_thread_init ();
 
$ Server = cerl_connect ('localhost: 100', 'hashserver ');
If (! $ Server)
Die ("ERROR: Connect HashSever failed! \ N ");

Printf (
"+ ----------------------------------------------------------- \ N ".
"| Put -Put (key, value) \ n ".
"| Get -Get value of a key \ n ".
"| Q-Quit \ n ".
"+ ----------------------------------------------------------- \ N ");
 
For (;;)
{
Echo "\ n> ";
List ($ cmd, $ key, $ value) = fscanf (STDIN, '% s % d % d ');
If ($ cmd = 'get' | $ cmd = 'g ')
{
$ Ret = $ server-> call (HashSever_get, $ key );
If ($ ret [0] = code_ OK)
Echo "result: $ ret [1] \ n ";
Else
Echo "error: $ ret [0] \ n ";
}
Else if ($ cmd = 'put' | $ cmd = 'p ')
{
$ Ret = $ server-> call (HashSever_put, $ key, $ value );
If ($ ret [0] = code_ OK)
Echo "OK! \ N ";
Else
Echo "error: $ ret [0] \ n ";
}
Else if ($ cmd = 'Q ')
{
Break;
}
Else
{
Echo "unknown command! \ N ";
}
}
 
Cerl_thread_term ();
?>
Define ('hashsever _ put', 1 );
Define ('hashsever _ get', 2 );

If (! Extension_loaded ('php _ cerl ')){
If (! Dl ('php _ cerl. '. PHP_SHLIB_SUFFIX ))
Die ("ERROR: Cannot load php_cerl library! \ N ");
}

Cerl_thread_init ();

$ Server = cerl_connect ('localhost: 100', 'hashserver ');
If (! $ Server)
Die ("ERROR: Connect HashSever failed! \ N ");
 
Printf (
"+ ----------------------------------------------------------- \ N ".
"| Put -Put (key, value) \ n ".
"| Get -Get value of a key \ n ".
"| Q-Quit \ n ".
"+ ----------------------------------------------------------- \ N ");

For (;;)
{
Echo "\ n> ";
List ($ cmd, $ key, $ value) = fscanf (STDIN, '% s % d % d ');
If ($ cmd = 'get' | $ cmd = 'g ')
{
$ Ret = $ server-> call (HashSever_get, $ key );
If ($ ret [0] = code_ OK)
Echo "result: $ ret [1] \ n ";
Else
Echo "error: $ ret [0] \ n ";
}
Else if ($ cmd = 'put' | $ cmd = 'p ')
{
$ Ret = $ server-> call (HashSever_put, $ key, $ value );
If ($ ret [0] = code_ OK)
Echo "OK! \ N ";
Else
Echo "error: $ ret [0] \ n ";
}
Else if ($ cmd = 'Q ')
{
Break;
}
Else
{
Echo "unknown command! \ N ";
}
}

Cerl_thread_term ();
?>
 

The following is a sample Client written in Python:

View plaincopy to clipboardprint?
HashSever_put = 1
HashSever_get = 2
 
Import sys, pycerl
 
Pycerl. thread_init ()
 
Server = pycerl. connect ("localhost: 8889", "HashServer ")
If not server:
Print "ERROR: Connect HashSever failed! "
 

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.