php實現(xiàn)與python進行socket通信的方法示例
本文實例講述了php實現(xiàn)與python進行socket通信的方法。分享給大家供大家參考,具體如下:
設(shè)計目的
通過前端頁面發(fā)起請求交給php,php創(chuàng)建socket請求交給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)閉服務</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)容感興趣的讀者可查看本站專題:《php socket用法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)學運算技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》及《PHP網(wǎng)絡(luò)編程技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- Python socket實現(xiàn)的簡單通信功能示例
- Python基于socket模塊實現(xiàn)UDP通信功能示例
- Python網(wǎng)絡(luò)編程使用select實現(xiàn)socket全雙工異步通信功能示例
- Python socket網(wǎng)絡(luò)編程TCP/IP服務器與客戶端通信
- 利用Python中SocketServer 實現(xiàn)客戶端與服務器間非阻塞通信
- 結(jié)合Python的SimpleHTTPServer源碼來解析socket通信
- python實現(xiàn)簡單socket通信的方法
- Python通過websocket與js客戶端通信示例分析
- python socket通信編程實現(xiàn)文件上傳代碼實例
相關(guān)文章
PHP實現(xiàn)提取多維數(shù)組指定一列的方法總結(jié)
這篇文章主要介紹了PHP實現(xiàn)提取多維數(shù)組指定一列的方法,結(jié)合實例形式總結(jié)分析了PHP針對多維數(shù)組的遍歷、轉(zhuǎn)換、提取等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
關(guān)于PHP中Object對象的筆記分享,學習php面向?qū)ο蟮呐笥芽梢詤⒖枷隆?/div> 2011-06-06
PHP實現(xiàn)多服務器session共享之NFS共享的方法
PHP實現(xiàn)多服務器session共享之NFS共享的方法...2007-03-03
php實現(xiàn)字符串首字母轉(zhuǎn)換成大寫的方法
這篇文章主要介紹了php實現(xiàn)字符串首字母轉(zhuǎn)換成大寫的方法,涉及php中ucfirst及ucwords函數(shù)的使用技巧,需要的朋友可以參考下2015-03-03最新評論

