国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php實現與python進行socket通信的方法示例

2024-05-04 23:59:55
字體:
來源:轉載
供稿:網友

本文實例講述了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

<?phprequire_once('mysocket.php');$con = Connector::getInstance();$req = "aaaaaaa";$con->sendMsg($req);$ret = $con->getMsg();echo $ret;?>

service / mysocket.php

<?phpclass 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 threadingimport socketimport timeencoding = 'utf-8'BUFSIZE = 1024# a read thread, read data from remoteclass 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 handleclass 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 threadlst.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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 弥渡县| 靖江市| 璧山县| 左云县| 玉林市| 潼关县| 威海市| 台安县| 专栏| 河南省| 梁山县| 井陉县| 昌邑市| 奉节县| 黄龙县| 洱源县| 临安市| 彭水| 晋中市| 青龙| 北海市| 哈尔滨市| 玉田县| 盐山县| 贺兰县| 赤峰市| 彭水| 台北县| 毕节市| 义乌市| 阳江市| 余庆县| 敦煌市| 辽阳县| 太仆寺旗| 碌曲县| 乐东| 隆德县| 甘泉县| 夏河县| 宜阳县|