Example of how php implements socket communication with python, pythonsocket
This example describes how php implements socket communication with python. We will share this with you for your reference. The details are as follows:
Purpose
Send a request to php through the front-end page. php creates a socket request and submits the request to the Python script. After the execution is complete, the request is returned to the front-end.
Index.html
<Html>
Service/main. php
<?phprequire_once('mysocket.php');$con = Connector::getInstance();$req = "aaaaaaa";$con->sendMsg($req);$ret = $con->getMsg();echo $ret;?>
Service/mysocket. php
<? Phpclass Connector {public static $ instance = null; public $ conn; private function _ construct () {set_time_limit (0); $ ip = '2017. 168.238.1 '; $ port = 8888; if ($ this-> conn = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) <0) {echo "socket_create () failure is caused: ". socket_strerror ($ this-> conn ). "\ n" ;}$ result = socket_connect ($ this-> conn, $ ip, $ port); if ($ result <0) {echo "socket_connect () failed. \ nReason :( $ Result )". socket_strerror ($ result ). "\ n" ;}else {echo "connection OK \ n" ;}} public static function getInstance () {if (is_null (self ::$ instance) {self :: $ instance = new Connector;} return self: $ instance;} public function sendMsg ($ msg) {socket_write ($ this-> conn, $ msg );} public function getMsg () {$ clients = array ($ this-> conn); while (true) {$ read = $ clients; $ wrSet = NULL; $ errSet = NULL; if (socket _ Select ($ read, $ wrSet, $ errSet, 3) <1) {continue;} foreach ($ read as $ read_sock) {$ data = @ socket_read ($ read_sock, 1024, PHP_BINARY_READ); socket_close ($ this-> conn); return $ data ;}}}?>
MultiServer. py
import threadingimport socketimport timeencoding = 'utf-8'BUFSIZE = 1024# a read thread, read data from remoteclass Reader(threading.Thread): def __init__(self, client): threading.Thread.__init__(self) self.client = client def run(self): #while True: data = self.client.recv(BUFSIZE) if(data): string = bytes.decode(data, encoding) print "from client::",string,"" time.sleep(10) self.client.send("return frome server::" + string) print "close:", self.client.getpeername() def readline(self): rec = self.inputs.readline() if rec: string = bytes.decode(rec, encoding) if len(string)>2: string = string[0:-2] else: string = ' ' else: string = False return string# a listen thread, listen remote connect# when a remote machine request to connect, it will create a read thread to handleclass Listener(threading.Thread): def __init__(self, port): threading.Thread.__init__(self) self.port = port self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind(("0.0.0.0", port)) self.sock.listen(0) def run(self): print "listener started" while True: client, cltadd = self.sock.accept() print "accept a connect..." Reader(client).start() cltadd = cltadd print "accept a connect(new reader..)"lst = Listener(8888) # create a listen threadlst.start() # then start# Now, you can use telnet to test it, the command is "telnet 127.0.0.1 9011"# You also can use web broswer to test, input the address of "http://127.0.0.1:9011" and press Enter button# Enjoy it....