I'm try to send and get data using sockets between Adobe flash client and python server. Flash client:
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("He开发者_开发问答llo World");
And python server:
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()
But this code not working. Python server output:
Connected by ('cl.ie.nt.ip', 3854)
Received '<policy-file-request/>\x00'
received policy
You shouldn't use the socket module if you don't have to. Use SocketServer when you want, well, a socket server.
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()
It should work this way ..
精彩评论