| Subcribe via RSS

Python与Flex之间Socket通讯

07月 12th, 2008 | No Comments | Posted in ActionScript3, Flex3, Python

在朋友的介绍下,接触了下Python,给人的感觉是,简洁、快速、规范,抽时间多学习学习

尝试了下使用Pyhon创建一简单的服务器,TCP方式,运行服务器,进入监听状态,然后在Flex中使用ActionScript3以Socket方式连接上去,将Flex中用户输入字符串发送到Python服务器,服务器接收到内容后加上时间戳再返回给Flex,显示给用户。

这只是一个非常简单学习的例子( ̄~ ̄;),其中,Python端代码源自《Core Python》一书,Flex端代码源自官方文档,根据需要作了一部分修改,不多说了,将Flex及Python代码贴上来,呵呵~~~

运行效果截图:

2008-07-12_181401

 

服务器端Python代码:

   1: #!/usr/bin/env python
   2: #coding=utf-8
   3: from socket import *
   4: from time import ctime
   5:  
   6: HOST=‘localhost’
   7: PORT=21567
   8: BUFSIZ=4096
   9: ADDR=(HOST, PORT)
  10:  
  11: tcpSerSock = socket(AF_INET, SOCK_STREAM)
  12: tcpSerSock.bind(ADDR)
  13: tcpSerSock.listen(5)
  14:  
  15: while True:
  16:     print ‘waiting for connection…’
  17:     tcpCliSock, addr = tcpSerSock.accept()
  18:     print ‘…connected from:’, addr
  19:  
  20:     while True:
  21:         data = tcpCliSock.recv(BUFSIZ)
  22:         if not data:
  23:             break
  24:         tcpCliSock.send(‘[%s] %s’ % (ctime(), data))
  25:  
  26:  
  27:         tcpCliSock.close()
  28: tcpSerSock.close()

客户端Flex代码:

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
   3:     creationComplete="init()">
   4: <mx:Script>
   5:     <![CDATA[
   6:         private var custSocket:Socket;
   7:         [Bindable] private var response:String = "";
   8:  
   9:         private function init():void {
  10:             custSocket = new Socket("localhost", 21567);
  11:             configureListeners();
  12:         }
  13:  
  14:         private function onClick(evt:Event):void {
  15:             sendRequest();
  16:         }
  17:  
  18:         private function configureListeners():void {
  19:             custSocket.addEventListener(Event.CLOSE, closeHandler);
  20:             custSocket.addEventListener(Event.CONNECT, connectHandler);
  21:             custSocket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  22:             custSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  23:             custSocket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
  24:         }
  25:  
  26:         private function writeln(str:String):void {
  27:             str += "\n";
  28:             try {
  29:                 custSocket.writeUTFBytes(str);
  30:             }
  31:             catch(e:IOError) {
  32:                 trace(e);
  33:             }
  34:         }
  35:  
  36:         private function sendRequest():void {
  37:             trace("sendRequest");
  38:             writeln(inTxt.text);
  39:             custSocket.flush();
  40:         }
  41:  
  42:         private function readResponse():void {
  43:             var str:String = custSocket.readUTFBytes(custSocket.bytesAvailable);
  44:             response += str;
  45:         }
  46:  
  47:         private function closeHandler(event:Event):void {
  48:             trace("closeHandler: " + event);
  49:             trace(response.toString());
  50:         }
  51:  
  52:         private function connectHandler(event:Event):void {
  53:             trace("connectHandler: " + event);
  54:         }
  55:  
  56:         private function ioErrorHandler(event:IOErrorEvent):void {
  57:             trace("ioErrorHandler: " + event);
  58:         }
  59:  
  60:         private function securityErrorHandler(event:SecurityErrorEvent):void {
  61:             trace("securityErrorHandler: " + event);
  62:         }
  63:  
  64:         private function socketDataHandler(event:ProgressEvent):void {
  65:             trace("socketDataHandler: " + event);
  66:             readResponse();
  67:         }
  68:  
  69:     ]]>
  70: </mx:Script>
  71:     <mx:TextArea text="{response}" id="outTxt"
  72:         height="126" width="283" fontSize="12"/>
  73:     <mx:HBox verticalAlign="bottom" width="282" height="40">
  74:         <mx:TextArea id="inTxt" width="100%" height="100%" fontSize="12"/>
  75:         <mx:Button label="发送"  fontSize="12" click="onClick(event)"/>
  76:     </mx:HBox>
  77:  
  78: </mx:Application>

Tags: