[英]Flash sockets with python server
I'm try to send and get data using sockets between Adobe flash client and python server. 我尝试使用Adobe Flash客户端和python服务器之间的套接字发送和获取数据。 Flash client:
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");
And python server: 和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()
But this code not working. 但是此代码无法正常工作。 Python server output:
Python服务器输出:
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.
如果需要套接字服务器,请使用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()
It should work this way .. 它应该这样工作..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.