php实现与python进行socket通信的方法示例
网络编程 2021-07-05 08:24www.168986.cn编程入门
这篇文章主要介绍了php实现与python进行socket通信的方法,结合实例形式分析了php使用自定义类发送socket请求数据及Python接收socket数据并处理请求等相关操作技巧,需要的朋友可以参考下
本文实例讲述了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_s_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_s_service()">关闭服务</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.aept() print "aept a connect..." Reader(client).start() cltadd = cltadd print "aept a connect(new reader..)" lst = Listener(8888) # create a listen thread lst.start() # then start # Now, you can use tel to test it, the mand is "tel 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程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程