2010-12-10 45 views
0

我嘗試使用Adobe Flash客戶端和python服務器之間的套接字發送和獲取數據。 Flash客戶端:使用python服務器的閃存套接字

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 2010-12-10 20:21:53