socket policy flash help

Piet van Oostrum piet at cs.uu.nl
Sun Aug 2 15:25:02 EDT 2009


>>>>> NighterNet <darkneter at gmail.com> (N) wrote:

>N> Here the full code.
>N> flashpolicy.xml
>N> [[[
>N> <?xml version="1.0"?>
>N> <cross-domain-policy>
>N>    <allow-access-from domain="*" to-ports="*" />
>N> </cross-domain-policy>
>N> ]]]

>N> flashpolicytest_server3x.py
>N> [[[

>N> #!/usr/local/bin/python
>N> '''
>N> Still under testing...
>N> python version 3.x.x
>N> '''
>N> import socket
>N> import threading
>N> import sys
>N> import os

>N> file_name = 'flashpolicy.xml'
>N> fh = open(file_name, "r")
>N> policy = fh.read(10001)

You never use that variable.

>N> host = ''; #out side network
>N> port = 5555;

>N> print ("#  ------------- Init... -------------  #");
>N> class ClientThread (threading.Thread):
>N> 	global policy;
>N> 	allClients = [];
>N> 	vlock = threading.Lock();
>N> 	id = 0 # next available thread number
>N> 	def __init__(self,clientSocket):
>N> 		threading.Thread.__init__(self)
>N> 		self.sockfd = clientSocket; #socket client
>N> 		self.name = '';
>N> 		ClientThread.id += 1
>N> 		self.id = ClientThread.id
>N> 		self.nickName = '';
>N> 		self.allClients.append(self.sockfd);
>N> 	def sendAll(self,buff):
>N> 		for index,clientSock in enumerate(self.allClients):
>N> 			try:
>N> 				clientSock.send(buff);

There is no guarantee that send will indeed transmit all of buff. It is
better to use clientSock.sendall(buff). Or you should check the return
value of send and check if it is equal to the length of buff. And repeat
if not. (But sendall is easier). 

Also don't use ; at the end of the statement. It's not pythonic.

>N> 			except (socket.error):
>N> 				print ('error socket %s\n',index,"| clean");
>N> 				clientSock.close()
>N> 				del self.allClients[index]
>N> 	def run(self):
>N> 		while True:
>N> 			buff = self.sockfd.recv(1028);

There is no guarantee that recv will get the whole message. It may even
get as little as 1 byte. So you should check the return value of recv.
The official way is to keep reading until you have enough input or until
you hit end of file.

>N> 			if not buff:
>N> 				print ("connect close...(client side)");
>N> 				self.sockfd.close();
>N> 				break #incase it loop infinite
>N> 			if str(buff) == str("b\'<policy-file-request/>\\x00\'"):

What you check here is whether buff contains the byte sequence that starts with a
b, then a ' ... and ending with the 5 bytes \ x 0 0 ' which is wrong.

Actually it should be:

if buff == b\'<policy-file-request/>\x00':
or
if buff == b\'<policy-file-request/>\0':


>N> 				print ('policy FOUND >>> sending...')
>N> 				print(buff)
>N> 				b = b'<?xml version=\"1.0\"?><cross-domain-policy><allow-access-
>N> from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>'
>N> 				print (b)
>N> 				self.sockfd.send(b);
>N> 				self.sockfd.sendall(b);

Only self.sockfd.sendall; delete the line with self.sockfd.send(b). And
remove the semicolons for esthetical reasons.

>N> Some odd reason I can't send flash policy from python to flash socket
>N> to agrees with the connection.

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list