php连接 python 套接字 socket通信
php连接 python 套接字 socket通信php 请求数据给pythonpython返回数据给phpphp 连接 python套接字的方法$ip = "127.0.0.1";$port = "5678";$sw = new stockConnector($ip,$port);$aa = ["status"=>true,"model"=>"12312","func"...
·
php连接 python 套接字 socket通信
php 请求数据给python
python返回数据给php
php 连接 python套接字的方法
$ip = "127.0.0.1";
$port = "5678";
$sw = new stockConnector($ip,$port);
$aa = ["status"=>true,"model"=>"12312","func"=>"asdas","params"=>"12312"];
$con = $sw->sendMsg(json_encode($aa));
$ret = $sw->getMsg();
dump($ret );
php 类
<?php
/**
链接套接字
2020-03-06
**/
namespace lib;
class stockConnector
{
public static $instance=null;
public $conn;
public function __construct($ip,$port)
{
set_time_limit(0);
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)
{
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 stockConnector;
}
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;
}
}
}
}
?>
python 连接python套接字的方法
import socket,json
if __name__ == '__main__':
port = "5678"
ip = "127.0.0.1"
client = socket.socket()
client.connect((ip, 5678)) # 连接到localhost主机的6969端口上去
print(client)
msg = {
'status': True,
# 'model':'scriptFunc.socket_test',
'model': 'scriptFunc.reload_model',
'func': 'nihao',
'params': ''
}
msg = json.dumps(msg)
client.send(msg.encode('utf-8')) # 把编译成utf-8的数据发送出去
data = client.recv(1024 * 5) # 接收客户发来的数据
print("接收到的命令为:", json.loads(data))
client.close()
python 接收端
import threading
import socket
import time
import json
encoding = 'utf-8'
BUFSIZE = 1024
# 读取线程,从远程读取数据
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, "")
msg = {
'status': "12312312"
}
# time.sleep(10)
msg = json.dumps(msg)
self.client.sendall(msg.encode('utf-8'))
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
# 监听线程,监听远程连接
# 当远程计算机请求连接时,它将创建一个要处理的读取线程
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(("127.0.0.1", 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(5678) # 创建侦听线程
lst.start() # 开始
# 现在,您可以使用telnet来测试它,命令是“telnet 127.0.0.19011”
# 您还可以使用web broswer进行测试,输入“http://127.0.0.1:9011”地址并按Enter按钮
更多推荐
已为社区贡献2条内容
所有评论(0)