socket policy flash help

N

NighterNet

I need help on the policy to able to let access to user to the server
once the policy access is finish. I been trying to find a good
example, but I got no luck. Using python version 3.1.

Here the code I tested but it not working.

if str(buff) == str("b\'<policy-file-request/>\\x00\'"):
print ('policy FOUND >>> sending...')
rawinput = str('<?xml version=\"1.0\"?><cross-domain-policy><allow-
access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>')
print (rawinput)
b = bytes ( ord(c) for c in rawinput)
self.sockfd.send(b);
 
P

paul

NighterNet said:
I need help on the policy to able to let access to user to the server
once the policy access is finish. I been trying to find a good
example, but I got no luck. Using python version 3.1.

Here the code I tested but it not working.

if str(buff) == str("b\'<policy-file-request/>\\x00\'"):
print ('policy FOUND >>> sending...')
rawinput = str('<?xml version=\"1.0\"?><cross-domain-policy><allow-
access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>')
print (rawinput)
b = bytes ( ord(c) for c in rawinput)
self.sockfd.send(b);
And the error is? Doesn't Flash use http as transport?

cheers
Paul
 
P

Piet van Oostrum

NighterNet said:
N> I need help on the policy to able to let access to user to the server
N> once the policy access is finish. I been trying to find a good
N> example, but I got no luck. Using python version 3.1.
N> Here the code I tested but it not working.
N> if str(buff) == str("b\'<policy-file-request/>\\x00\'"):

What is buff supposed to contain here? I assume a byte sequence?
Do you reaaly mean that buff[0] contains the byte 'b' and buff[1]
contains a single quote? And the last byte also a single quote? And the
four bytes with \ x 0 0 before that instead of a null byte? In total 29 bytes?

Or did you mean just the 23 bytes long b'<policy-file-request/>\x00'?

Maybe it should be if buff == b' said:
N> print ('policy FOUND >>> sending...')
N> rawinput = str('<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>')

The str here is unnecessary as you have already a string.
N> print (rawinput)
N> b = bytes ( ord(c) for c in rawinput)

Why not
 
N

NighterNet

N> I need help on the policy to able to let access to user to the server
N> once the policy access is finish. I been trying to find a good
N> example, but I got no luck. Using python version 3.1.
N> Here the code I tested but it not working.
N> if str(buff) == str("b\'<policy-file-request/>\\x00\'"):

What is buff supposed to contain here? I assume a byte sequence?
Do you reaaly mean that buff[0] contains the byte 'b' and buff[1]
contains a single quote? And the last byte also a single quote? And the
four bytes with \ x 0 0 before that instead of a null byte? In total 29 bytes?

Or did you mean just the 23 bytes long b'<policy-file-request/>\x00'?

Maybe it should be if buff == b' said:
N>                               print ('policy FOUND >>> sending...')
N>                               rawinput = str('<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>')

The str here is unnecessary as you have already a string.
N>                               print (rawinput)
N>                               b = bytes ( ord(c) for c in rawinput)

Why not
b = b' said:
N>                               self.sockfd.send(b);


I try that method and flash socket is not getting the policy.

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

server = socket.socket( socket.AF_INET, socket.SOCK_STREAM)

When it able to read the policy it send it but it fail to send to
flash some reason and send it again and then d/c.
 
N

NighterNet

Here the full code.

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

flashpolicytest_server3x.py
[[[

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

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

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

print ("# ------------- Init... ------------- #");
class ClientThread (threading.Thread):
global policy;
allClients = [];
vlock = threading.Lock();
id = 0 # next available thread number
def __init__(self,clientSocket):
threading.Thread.__init__(self)
self.sockfd = clientSocket; #socket client
self.name = '';
ClientThread.id += 1
self.id = ClientThread.id
self.nickName = '';
self.allClients.append(self.sockfd);
def sendAll(self,buff):
for index,clientSock in enumerate(self.allClients):
try:
clientSock.send(buff);
except (socket.error):
print ('error socket %s\n',index,"| clean");
clientSock.close()
del self.allClients[index]
def run(self):
while True:
buff = self.sockfd.recv(1028);
if not buff:
print ("connect close...(client side)");
self.sockfd.close();
break #incase it loop infinite
if str(buff) == str("b\'<policy-file-request/>\\x00\'"):
print ('policy FOUND >>> sending...')
print(buff)
b = b'<?xml version=\"1.0\"?><cross-domain-policy><allow-access-
from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>'
print (b)
self.sockfd.send(b);
self.sockfd.sendall(b);
print(buff);
self.sendAll(buff)
self.sockfd.close()
print ("# ------------- Init... Listen Client ------------- #\n");
try:
server = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
except AttributeError:
# AttributeError catches Python built without IPv6
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
# socket.error catches OS with IPv6 disabled
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((host,port))
server.listen(5)
print ("Server Up Listen!",host,":",port," Bind!");
while True:
(clientSocket, address) = server.accept();
print("client connect from :",address);
ct = ClientThread(clientSocket);
print(ct.id);
ct.start();

]]]

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

Piet van Oostrum

NighterNet said:
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
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

NighterNet

NighterNet <[email protected]> (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> importsocket
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; #socketclient
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 ('errorsocket%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\' said:
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 flashsocket
N> to agrees with the connection.

thanks it help. I was not sure about the format most the time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top