2010-12-10 14 views
0

Adob​​e FlashクライアントとPythonサーバー間のソケットを使用してデータを送信して取得しようとしています。 Flashクライアント:Pythonサーバーを搭載したFlashソケット

var serverURL = "se.rv.er.ip"; 
var xmlSocket:XMLSocket = new XMLSocket(); 
xmlSocket.connect(serverURL, 50007); 

xmlSocket.addEventListener(DataEvent.DATA, onIncomingData); 

function onIncomingData(event:DataEvent):void 
{ 
    trace("[" + event.type + "] " + event.data); 
} 

xmlSocket.send("Hello World"); 

とPythonサーバ:

import socket 

HOST = ''     # Symbolic name meaning all available interfaces 
PORT = 50007    # Arbitrary non-privileged port 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.bind((HOST, PORT)) 
s.listen(1) 
conn, addr = s.accept() 
print 'Connected by', addr 
while 1: 
    data = conn.recv(1024) 
    if (data): 
     print 'Received', repr(data) 
     # data 
     if(str(repr(data)).find('<policy-file-request/>')!=-1): 
      print 'received policy' 
      conn.send('<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="*" to-ports="50007" /></cross-domain-policy>') 
      conn.send('hellow wolrd') 
conn.close() 

しかし、このコードは動作しません。 Pythonのサーバー出力:

Connected by ('cl.ie.nt.ip', 3854) 
Received '<policy-file-request/>\x00' 
received policy 

答えて

1

あなたがする必要がない場合は、ソケットモジュールを使用しないでください。よく、ソケットサーバーが必要な場合は、SocketServerを使用してください。

import SocketServer 

class MyTCPHandler(SocketServer.BaseRequestHandler): 
    def handle(self): 
      # self.request is the TCP socket connected to the client 
      self.data = self.request.recv(1024).strip() 
      print "%s wrote:" % self.client_address[0] 
      print self.data 
      if '<policy-file-request/>' in self.data: 
       print 'received policy' 
       conn.send('<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="*" to-ports="50007" /></cross-domain-policy>') 
       conn.send('hellow wolrd') 

def main(): 
    # Create the server, binding to localhost on port 9999 
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) 

    # Activate the server; this will keep running until you 
    # interrupt the program with Ctrl-C 
    server.serve_forever() 

それがこのように動作するはずです。..

+0

私はPythonスクリプトに問題がないと思いますが、あなたとスクリプトは、私はまた、127.0.0.1が書いた取得: を がポリシーを受信して​​いませんもっと – nucleartux

関連する問題