无为清净楼资源网 Design By www.qnjia.com
本文实例讲述了php实现与python进行socket通信的方法。分享给大家供大家参考,具体如下:
设计目的
通过前端页面发起请求交给php,php创建socket请求交给Python脚本,然后执行完毕之后,返回给前端。
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",true); g_xmlHttpReq.onreadystatechange=onReplyCallback; g_xmlHttpReq.send(null); } </script> </head> <body> <button onclick="on_stop_service()">关闭服务</button> </body> </html>
service / main.php
<"aaaaaaa"; $con->sendMsg($req); $ret = $con->getMsg(); echo $ret; "htmlcode"><"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; } } } } "htmlcode">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....更多关于PHP相关内容感兴趣的读者可查看本站专题:《php socket用法总结》、《php字符串(string)用法总结》、《PHP数学运算技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《PHP网络编程技巧总结》
希望本文所述对大家PHP程序设计有所帮助。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】