socket policy flash help
Piet van Oostrum
piet at cs.uu.nl
Sun Aug 2 21:25:02 CEST 2009
>>>>> NighterNet <darkneter at gmail.com> (N) wrote:
>N> Here the full code.
>N> <?xml version="1.0"?>
>N> <allow-access-from domain="*" to-ports="*" />
>N> Still under testing...
>N> python version 3.x.x
>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> self.sockfd = clientSocket; #socket client
>N> self.name = '';
>N> ClientThread.id += 1
>N> self.id = ClientThread.id
>N> self.nickName = '';
>N> def sendAll(self,buff):
>N> for index,clientSock in enumerate(self.allClients):
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> 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> 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':
if buff == b\'<policy-file-request/>\0':
>N> print ('policy FOUND >>> sending...')
>N> b = b'<?xml version=\"1.0\"?><cross-domain-policy><allow-access-
>N> from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>'
>N> print (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