File: policyserver.py

package info (click to toggle)
gevent-socketio 0.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 716 kB
  • ctags: 551
  • sloc: python: 2,031; makefile: 134
file content (27 lines) | stat: -rw-r--r-- 1,051 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from gevent.server import StreamServer
import socket

__all__ = ['FlashPolicyServer']


class FlashPolicyServer(StreamServer):
    policyrequest = "<policy-file-request/>"
    policy = """<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>"""

    def __init__(self, listener=None, backlog=None):
        if listener is None:
            listener = ('0.0.0.0', 10843)
        StreamServer.__init__(self, listener=listener, backlog=backlog)

    def handle(self, sock, address):
        # send and read functions should not wait longer than three seconds
        sock.settimeout(3)
        try:
            # try to receive at most 128 bytes (`POLICYREQUEST` is shorter)
            input = sock.recv(128)
            if input.startswith(FlashPolicyServer.policyrequest):
                sock.sendall(FlashPolicyServer.policy)
        except socket.timeout:
            pass
        sock.close()