未加星标

php实现与python进行socket通信的方法示例

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

本文实例讲述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?cmd=1",true);
g_xmlHttpReq.onreadystatechange=onReplyCallback;
g_xmlHttpReq.send(null);
}
</script>
</head>
<body>
<button onclick="on_stop_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.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程序设计有所帮助。

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: PHP算法数据结构XMLUF数据RYPython
分页:12
转载请注明
本文标题:php实现与python进行socket通信的方法示例
本站链接:http://www.codesec.net/view/563711.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(34)