php實(shí)現(xiàn)與python進(jìn)行socket通信的方法示例
本文實(shí)例講述了php實(shí)現(xiàn)與python進(jìn)行socket通信的方法。分享給大家供大家參考,具體如下:
設(shè)計(jì)目的
通過(guò)前端頁(yè)面發(fā)起請(qǐng)求交給php,php創(chuàng)建socket請(qǐng)求交給Python腳本,然后執(zhí)行完畢之后,返回給前端。
index.html
<html> <head> <title>test</title> <script> g_xmlHttpReq = new XMLHttpRequest(); function onReplyCallback() { if(g_xmlHttpReq.readyState==4 && g_xmlHttpReq.status==200) { alert(g_xmlHttpReq.responseText); } } function on_stop_service() { g_xmlHttpReq.open("GET","./service/main.php?cmd=1",true); g_xmlHttpReq.onreadystatechange=onReplyCallback; g_xmlHttpReq.send(null); } </script> </head> <body> <button onclick="on_stop_service()">關(guān)閉服務(wù)</button> </body> </html>
service / main.php
<?php require_once('mysocket.php'); $con = Connector::getInstance(); $req = "aaaaaaa"; $con->sendMsg($req); $ret = $con->getMsg(); echo $ret; ?>
service / mysocket.php
<?php class Connector { public static $instance=null; public $conn; private function __construct() { set_time_limit(0); $ip = '192.168.238.1'; $port = 8888; if(($this->conn = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) { echo "socket_create() 失敗的原因是:".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 "連接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 threading import socket import time encoding = 'utf-8' BUFSIZE = 1024 # a read thread, read data from remote class 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 handle class 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 thread lst.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....
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php socket用法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》及《PHP網(wǎng)絡(luò)編程技巧總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- Python socket實(shí)現(xiàn)的簡(jiǎn)單通信功能示例
- Python基于socket模塊實(shí)現(xiàn)UDP通信功能示例
- Python網(wǎng)絡(luò)編程使用select實(shí)現(xiàn)socket全雙工異步通信功能示例
- Python socket網(wǎng)絡(luò)編程TCP/IP服務(wù)器與客戶(hù)端通信
- 利用Python中SocketServer 實(shí)現(xiàn)客戶(hù)端與服務(wù)器間非阻塞通信
- 結(jié)合Python的SimpleHTTPServer源碼來(lái)解析socket通信
- python實(shí)現(xiàn)簡(jiǎn)單socket通信的方法
- Python通過(guò)websocket與js客戶(hù)端通信示例分析
- python socket通信編程實(shí)現(xiàn)文件上傳代碼實(shí)例
相關(guān)文章
PHP實(shí)現(xiàn)提取多維數(shù)組指定一列的方法總結(jié)
這篇文章主要介紹了PHP實(shí)現(xiàn)提取多維數(shù)組指定一列的方法,結(jié)合實(shí)例形式總結(jié)分析了PHP針對(duì)多維數(shù)組的遍歷、轉(zhuǎn)換、提取等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12關(guān)于PHP中Object對(duì)象的筆記分享
關(guān)于PHP中Object對(duì)象的筆記分享,學(xué)習(xí)php面向?qū)ο蟮呐笥芽梢詤⒖枷隆?/div> 2011-06-06php 多個(gè)變量指向同一個(gè)引用($b = &$a)用法分析
這篇文章主要介紹了php 多個(gè)變量指向同一個(gè)引用($b = &$a)用法,結(jié)合實(shí)例形式分析了PHP變量引用原理、優(yōu)缺點(diǎn)及相關(guān)操作技巧,需要的朋友可以參考下2019-11-11PHP實(shí)現(xiàn)多服務(wù)器session共享之NFS共享的方法
PHP實(shí)現(xiàn)多服務(wù)器session共享之NFS共享的方法...2007-03-03php 獲取頁(yè)面中指定內(nèi)容的實(shí)現(xiàn)類(lèi)
本文為大家下使用php如何獲取頁(yè)面中的指定內(nèi)容,而且以封裝成類(lèi),需要的朋友可以參考下本文2014-01-01php實(shí)現(xiàn)字符串首字母轉(zhuǎn)換成大寫(xiě)的方法
這篇文章主要介紹了php實(shí)現(xiàn)字符串首字母轉(zhuǎn)換成大寫(xiě)的方法,涉及php中ucfirst及ucwords函數(shù)的使用技巧,需要的朋友可以參考下2015-03-03PHP 數(shù)字左側(cè)自動(dòng)補(bǔ)0
舉例來(lái)說(shuō):?jiǎn)T工的工號(hào) X001 雖然 MySQL 本身的字段設(shè)定成 zerofill 的屬性時(shí),會(huì)將不足位數(shù)的部份補(bǔ)上0,但是這個(gè)功能只能用在數(shù)字的字段上面。 所以必要的時(shí)候只好靠PHP來(lái)進(jìn)行了。2008-03-03最新評(píng)論