コード例 #1
0
ファイル: redis.py プロジェクト: dowski/aspen
 def _send(self, cmd, *args, **kwargs):
     if 'list' in kwargs:
         args = kwargs['list']
     all = (cmd, ) + tuple(str(s) for s in args)
     send('*%s\r\n' % len(all))
     for i in all:
         send(('$%s\r\n' % len(i)) + i + '\r\n')
コード例 #2
0
ファイル: smtp.py プロジェクト: tewks/dsmtp
 def putcmd(self, cmd, args=""):
     """Send a command to the server."""
     if args == "":
         str = '%s%s' % (cmd, CRLF)
     else:
         str = '%s %s%s' % (cmd, args, CRLF)
     send(str)
コード例 #3
0
ファイル: beanstalkc.py プロジェクト: dowski/aspen
 def close(self):
     """Gracefully close beanstalk connection"""
     try:
         diesel.send('quit\r\n')
         diesel.Client.close(self)
     except diesel.ConnectionClosed:
         pass
コード例 #4
0
ファイル: beanstalkc.py プロジェクト: tanvir-tech/Network_Lab
 def close(self):
     """Gracefully close beanstalk connection"""
     try:
         diesel.send('quit\r\n')
         diesel.Client.close(self)
     except diesel.ConnectionClosed:
         pass
コード例 #5
0
ファイル: echo_timeout.py プロジェクト: dowski/aspen
def hi_server(addr):
    while 1:
        ev, val = first(until_eol=True, sleep=3)
        if ev == 'sleep':
            log.warn('%s timeout!' % time.asctime())
        else:
            send("you said %s" % val)
コード例 #6
0
ファイル: sleep_server.py プロジェクト: brucezhangf/diesel-1
def delay_echo_server(addr):
    inp = until_eol()

    for x in xrange(4):
        sleep(2)
        send(str(x) + '\r\n')
    send("you said %s" % inp)
コード例 #7
0
ファイル: redis.py プロジェクト: viraptor/diesel
 def _send_bulk_multi(self, cmd, *args, **kwargs):
     if 'list' in kwargs:
         args = kwargs['list']
     all = (cmd,) + tuple(args)
     send('*%s\r\n' % len(all))
     for i in all:
         send(('$%s\r\n' % len(i)) + i + '\r\n')
コード例 #8
0
ファイル: riak.py プロジェクト: arnaudsj/diesel
 def info(self):
     # No protocol buffer object to build or send.
     message_code = 7
     total_size = 1
     fmt = "!iB"
     diesel.send(struct.pack(fmt, total_size, message_code))
     return self._receive()
コード例 #9
0
ファイル: DNS.py プロジェクト: 1angxi/diesel
 def _actually_resolve(self, name, timeout):
     timeout = timeout / float(len(self.nameservers))
     try:
         for server in self.nameservers:
             # Try each nameserver in succession.
             self.addr = server
             query = make_query(name, A)
             send(query.to_wire())
             start = time.time()
             remaining = timeout
             while True:
                 # Handle the possibility of responses that are not to our
                 # original request - they are ignored and we wait for a
                 # response that matches our query.
                 item, data = first(datagram=True, sleep=remaining)
                 if item == "datagram":
                     response = from_wire(data)
                     if query.is_response(response):
                         if response.answer:
                             a_records = [r for r in response.answer if r.rdtype == A]
                             return [item.address for item in a_records[0].items]
                         raise NotFound
                     else:
                         # Not a response to our query - continue waiting for
                         # one that is.
                         remaining = remaining - (time.time() - start)
                 elif item == "sleep":
                     break
         else:
             raise Timeout(name)
     finally:
         self.addr = self.primary
コード例 #10
0
ファイル: riak.py プロジェクト: brucezhangf/diesel-1
 def info(self):
     # No protocol buffer object to build or send.
     message_code = 7
     total_size = 1
     fmt = "!iB"
     diesel.send(struct.pack(fmt, total_size, message_code))
     return self._receive()
コード例 #11
0
def hi_server(addr):
    while 1:
        ev, val = first(until_eol=True, sleep=3)
        if ev == 'sleep':
            log.warn('%s timeout!' % time.asctime())
        else:
            send("you said %s" % val)
コード例 #12
0
ファイル: websockets.py プロジェクト: zorkian/diesel
    def handle_rfc_6455_frames(self, inq, outq):
        disconnecting = False
        while True:
            typ, val = first(receive=2, waits=[outq])
            if typ == 'receive':
                b1, b2 = unpack(">BB", val)

                opcode = b1 & 0x0f
                fin = (b1 & 0x80) >> 7
                has_mask = (b2 & 0x80) >> 7

                assert has_mask == 1, "Frames must be masked"

                if opcode == 8:
                    inq.put(WebSocketDisconnect())
                    if disconnecting:
                        break
                    disconnecting = True
                assert opcode in [1,8], "Currently only opcodes 1 & 8 are supported (opcode=%s)" % opcode
                length = b2 & 0x7f
                if length == 126:
                    length = unpack('>H', receive(2))[0]
                elif length == 127:
                    length = unpack('>L', receive(8))[0]

                mask = unpack('>BBBB', receive(4))
                if length:
                    payload = array('B', receive(length))
                    if disconnecting:
                        continue
                    for i in xrange(len(payload)):
                        payload[i] ^= mask[i % 4]

                    try:
                        data = loads(payload.tostring())
                        inq.put(data)
                    except JSONDecodeError:
                        pass
            elif typ == outq:
                if type(val) is WebSocketDisconnect:
                    b1 = 0x80 | (8 & 0x0f) # FIN + opcode
                    send(pack('>BB', b1, 0))
                    if disconnecting:
                        break
                    disconnecting = True
                else:
                    payload = dumps(val)

                    b1 = 0x80 | (1 & 0x0f) # FIN + opcode

                    payload_len = len(payload)
                    if payload_len <= 125:
                        header = pack('>BB', b1, payload_len)
                    elif payload_len > 125 and payload_len < 65536:
                        header = pack('>BBH', b1, 126, payload_len)
                    elif payload_len >= 65536:
                        header = pack('>BBQ', b1, 127, payload_len)

                    send(header + payload)
コード例 #13
0
ファイル: test_service.py プロジェクト: dowski/aspen
        def handle_echo(conn):
            size = int((until('|'))[:-1])
            said = receive(size)
            out = 'YS:' + said

            send(out)

            touch()
コード例 #14
0
ファイル: dreadlock.py プロジェクト: brucezhangf/diesel-1
 def unlock(self, key):
     send("unlock %s\r\n" % (key, ))
     response = until_eol()
     if response[0] == 'u':
         return
     if response[0] == 'e':
         raise DreadlockError(response[2:].strip())
     assert False, response
コード例 #15
0
ファイル: riak.py プロジェクト: brucezhangf/diesel-1
 def _send(self, pb):
     # Send a protocol buffer on the wire as a request.
     message_code = PB_TO_MESSAGE_CODE[pb.__class__]
     message = pb.dumps()
     message_size = len(message)
     total_size = message_size + 1  # plus 1 mc byte
     fmt = "!iB%ds" % message_size
     diesel.send(struct.pack(fmt, total_size, message_code, message))
コード例 #16
0
ファイル: riak.py プロジェクト: arnaudsj/diesel
 def _send(self, pb):
     # Send a protocol buffer on the wire as a request.
     message_code = PB_TO_MESSAGE_CODE[pb.__class__]
     message = pb.dumps()
     message_size = len(message)
     total_size = message_size + 1 # plus 1 mc byte
     fmt = "!iB%ds" % message_size
     diesel.send(struct.pack(fmt, total_size, message_code, message))
コード例 #17
0
ファイル: dreadlock.py プロジェクト: 1angxi/diesel
 def unlock(self, key):
     send("unlock %s\r\n" % (key,))
     response = until_eol()
     if response[0] == 'u':
         return
     if response[0] == 'e':
         raise DreadlockError(response[2:].strip())
     assert False, response
コード例 #18
0
ファイル: test_service.py プロジェクト: viraptor/diesel
        def handle_echo(conn):
            size = int((until('|'))[:-1])
            said = count(size)
            out = 'YS:' + said

            send( out )

            touch()
コード例 #19
0
ファイル: consolechat.py プロジェクト: 1angxi/diesel
def chat_server(addr):
    my_nick = until_eol().strip()
    while True:
        evt, data = first(until_eol=True, waits=["chat_message"])
        if evt == "until_eol":
            fire("chat_message", (my_nick, data.strip()))
        else:
            nick, message = data
            send("<%s> %s\r\n" % (nick, message))
コード例 #20
0
ファイル: consolechat.py プロジェクト: brucezhangf/diesel-1
def chat_server(addr):
    my_nick = until_eol().strip()
    while True:
        evt, data = first(until_eol=True, waits=['chat_message'])
        if evt == 'until_eol':
            fire('chat_message', (my_nick, data.strip()))
        else:
            nick, message = data
            send("<%s> %s\r\n" % (nick, message))
コード例 #21
0
ファイル: test_service.py プロジェクト: viraptor/diesel
        def handle_echo(conn):
            said = until_eol()
            out = 'YS:' + said

            send( out )
            for c in out:
                send( c )

            touch()
コード例 #22
0
ファイル: chat.py プロジェクト: dowski/aspen
def chat_server(addr):
    my_nick = until_eol().strip()
    while True:
        ev, val = first(until_eol=True, waits=['chat_message'])
        if ev == 'until_eol':
            fire('chat_message', (my_nick, val.strip()))
        else:
            nick, message = val
            send("<%s> %s\r\n"  % (nick, message))
コード例 #23
0
ファイル: test_service.py プロジェクト: dowski/aspen
        def handle_echo(conn):
            said = until_eol()
            out = 'YS:' + said

            send( out )
            for c in out:
                send( c )

            touch()
コード例 #24
0
ファイル: http.py プロジェクト: wmoss/diesel
    def request(self, method, path, headers, body=None, timeout=None):
        '''Issues a `method` request to `path` on the
        connected server.  Sends along `headers`, and
        body.

        Very low level--you must set "host" yourself,
        for example.  It will set Content-Length,
        however.
        '''
        timeout_handler = TimeoutHandler(timeout or 60)
        req = HttpRequest(method, path, '1.1')

        if body:
            headers.set('Content-Length', len(body))

        send('%s\r\n%s\r\n\r\n' % (req.format(),
        headers.format()))

        if body:
            send(body)

        ev, val = first(until_eol=True, sleep=timeout_handler.remaining())
        if ev == 'sleep': timeout_handler.timeout()

        resp_line = val

        try:
            version, code, reason = resp_line.split(None, 2)
        except ValueError:
            # empty reason string
            version, code = resp_line.split(None, 1)
            reason = ''

        code = int(code)

        ev, val = first(until="\r\n\r\n", sleep=timeout_handler.remaining())
        if ev == 'sleep': timeout_handler.timeout()

        header_block = val

        heads = HttpHeaders()
        heads.parse(header_block)

        if method == 'HEAD':
            body = None
        elif heads.get_one('Transfer-Encoding') == 'chunked':
            body = handle_chunks(heads, timeout_handler.remaining())
        elif heads.get_one('Connection') == 'close' and 'Content-Length' not in heads:
            body = ''
            try:
                while True:
                    s = receive(2**16)
                    body += s
            except ConnectionClosed, e:
                if e.buffer:
                    body += e.buffer
コード例 #25
0
ファイル: http.py プロジェクト: HVF/diesel
    def request(self, method, url, headers={}, body=None, timeout=None):
        '''Issues a `method` request to `path` on the
        connected server.  Sends along `headers`, and
        body.

        Very low level--you must set "host" yourself,
        for example.  It will set Content-Length,
        however.
        '''
        url_info = urlparse(url)
        fake_wsgi = dict(
        (cgi_name(n), v) for n, v in headers.iteritems())
        fake_wsgi.update({
            'HTTP_METHOD' : method,
            'SCRIPT_NAME' : '',
            'PATH_INFO' : url_info[2],
            'QUERY_STRING' : url_info[4],
            'wsgi.version' : (1,0),
            'wsgi.url_scheme' : 'http', # XXX incomplete
            'wsgi.input' : cStringIO.StringIO(body or ''),
            'wsgi.errors' : FileLikeErrorLogger(hlog),
            'wsgi.multithread' : False,
            'wsgi.multiprocess' : False,
            'wsgi.run_once' : False,
            })
        req = Request(fake_wsgi)

        timeout_handler = TimeoutHandler(timeout or 60)

        send('%s %s HTTP/1.1\r\n%s' % (req.method, req.url, str(req.headers)))

        if body:
            send(body)

        h = HttpParser()
        body = []
        data = None
        while True:
            if data:
                used = h.execute(data, len(data))
                if h.is_headers_complete():
                    body.append(h.recv_body())
                if h.is_message_complete():
                    data = data[used:]
                    break
            ev, val = first(receive_any=True, sleep=timeout_handler.remaining())
            if ev == 'sleep': timeout_handler.timeout()
            data = val

        resp = Response(
            response=''.join(body),
            status=h.get_status_code(),
            headers=h.get_headers(),
            )

        return resp
コード例 #26
0
ファイル: http.py プロジェクト: wmoss/diesel
    def request(self, method, path, headers, body=None, timeout=None):
        '''Issues a `method` request to `path` on the
        connected server.  Sends along `headers`, and
        body.

        Very low level--you must set "host" yourself,
        for example.  It will set Content-Length,
        however.
        '''
        timeout_handler = TimeoutHandler(timeout or 60)
        req = HttpRequest(method, path, '1.1')

        if body:
            headers.set('Content-Length', len(body))

        send('%s\r\n%s\r\n\r\n' % (req.format(), headers.format()))

        if body:
            send(body)

        ev, val = first(until_eol=True, sleep=timeout_handler.remaining())
        if ev == 'sleep': timeout_handler.timeout()

        resp_line = val

        try:
            version, code, reason = resp_line.split(None, 2)
        except ValueError:
            # empty reason string
            version, code = resp_line.split(None, 1)
            reason = ''

        code = int(code)

        ev, val = first(until="\r\n\r\n", sleep=timeout_handler.remaining())
        if ev == 'sleep': timeout_handler.timeout()

        header_block = val

        heads = HttpHeaders()
        heads.parse(header_block)

        if method == 'HEAD':
            body = None
        elif heads.get_one('Transfer-Encoding') == 'chunked':
            body = handle_chunks(heads, timeout_handler.remaining())
        elif heads.get_one(
                'Connection') == 'close' and 'Content-Length' not in heads:
            body = ''
            try:
                while True:
                    s = receive(2**16)
                    body += s
            except ConnectionClosed, e:
                if e.buffer:
                    body += e.buffer
コード例 #27
0
 def handler(self, remote_addr):
     host, port = remote_addr[0], remote_addr[1]
     print "Echo client connected from: %s:%d" % (host, port)
     while True:
         try:
             msg = diesel.until_eol()
             y = ': '.join(["you said", msg])
             diesel.send(y)
         except Exception as e:
             print e
コード例 #28
0
ファイル: consolechat.py プロジェクト: brucezhangf/diesel-1
 def chat(self):
     fork(self.input_handler)
     nick = self.input.get()
     send("%s\r\n" % nick)
     while True:
         evt, data = first(until_eol=True, waits=[self.input])
         if evt == "until_eol":
             print data.strip()
         else:
             send("%s\r\n" % data)
コード例 #29
0
 def handler(self, remote_addr):
     host, port = remote_addr[0], remote_addr[1]
     print('Echo client connected from: %s:%d' % (host, port))
     while True:
         try:
             message = diesel.until_eol()
             your_message = ': '.join(['You said', message])
             diesel.send(your_message)
         except Exception as e:
             print('Exception:', e)
コード例 #30
0
ファイル: use_diesel.py プロジェクト: BeginMan/pytool
 def handler(self, remote_addr):
     host, port = remote_addr[0], remote_addr[1]
     print "Echo client connected from: %s:%d" % (host, port)
     while True:
         try:
             msg = diesel.until_eol()
             y = ': '.join(["you said", msg])
             diesel.send(y)
         except Exception as e:
             print e
コード例 #31
0
ファイル: memcache.py プロジェクト: pranjal5215/diesel
 def get(self, k):
     send('get %s\r\n'%str(k))
     resp = self._get_response()
     value = None
     if resp:
         key ,value = resp
         # If we have received a valid response for the
         # last key, only then fetch from socket.
         self._get_response()
     return value
コード例 #32
0
ファイル: mongodb.py プロジェクト: arnaudsj/diesel
 def send(self, data, respond=False):
     """Send raw mongodb data and optionally yield the server's response."""
     send(data)
     if not respond:
         return ''
     else:
         header = receive(HEADER_SIZE)
         length, id, to, opcode = struct.unpack('<4i', header)
         body = receive(length - HEADER_SIZE)
         return header + body
コード例 #33
0
ファイル: consolechat.py プロジェクト: 1angxi/diesel
 def chat(self):
     fork(self.input_handler)
     nick = self.input.get()
     send("%s\r\n" % nick)
     while True:
         evt, data = first(until_eol=True, waits=[self.input])
         if evt == "until_eol":
             print data.strip()
         else:
             send("%s\r\n" % data)
コード例 #34
0
 def send(self, data, respond=False):
     """Send raw mongodb data and optionally yield the server's response."""
     send(data)
     if not respond:
         return ''
     else:
         header = receive(HEADER_SIZE)
         length, id, to, opcode = struct.unpack('<4i', header)
         body = receive(length - HEADER_SIZE)
         return header + body
コード例 #35
0
ファイル: dreadlock.py プロジェクト: brucezhangf/diesel-1
 def lock(self, key, timeout=10.0):
     ms_timeout = int(timeout * 1000)
     send("lock %s %d\r\n" % (key, ms_timeout))
     response = until_eol()
     if response[0] == 'l':
         return
     if response[0] == 't':
         raise DreadlockTimeout(key)
     if response[0] == 'e':
         raise DreadlockError(response[2:].strip())
     assert False, response
コード例 #36
0
ファイル: dreadlock.py プロジェクト: 1angxi/diesel
 def lock(self, key, timeout=10.0):
     ms_timeout = int(timeout * 1000)
     send("lock %s %d\r\n" % (key, ms_timeout))
     response = until_eol()
     if response[0] == 'l':
         return
     if response[0] == 't':
         raise DreadlockTimeout(key)
     if response[0] == 'e':
         raise DreadlockError(response[2:].strip())
     assert False, response
コード例 #37
0
    def handler(self,remote_addr):
        host,port = remote_addr[0],remote_addr[1]
        print "[*] Echo client connected from: %s:%d" %(host,port)

        while True:
            try:
                message = diesel.until_eol()
                your_message = ": ".join(["You said",message])
                diesel.send(your_message)
            except Exception,e:
                print "[-] Exception:",e
 def handler(self, remote_addr):
     host, port = remote_addr[0], remote_addr[1]
     print "Echo Client connected from: %s:%d" % (host, port)
     while True:
         try:
             message = diesel.until_eol()
             your_message = ':'.join(['You said', message])
             diesel.send(your_message)
         except Exception, e:
             print "error:", e
             break
コード例 #39
0
    def keep_clients_alive(self):
        alive = []
        TIMEOUT = 15  # XXX
        now = time.time()
        for c, t in clients.items():
            if c is None or time.time() - t > TIMEOUT:
                del clients[c]

        send("PEER-KEEPALIVE %s\r\n" % (' '.join(clients)))
        res = until_eol().strip().upper()
        assert res == "PEER-KEEPALIVE-OKAY"
コード例 #40
0
ファイル: console.py プロジェクト: 1angxi/diesel
 def handle_command(self):
     header = diesel.receive(8)
     (sz,) = struct.unpack('>Q', header)
     data = diesel.receive(sz)
     stdout_patch = StdoutDispatcher()
     with stdout_patch:
         self.interpreter.runsource(data)
     output = stdout_patch.contents
     outsz = len(output)
     outheader = struct.pack('>Q', outsz)
     diesel.send("%s%s" % (outheader, output))
コード例 #41
0
 def handler(self, remote_addr):
     host, port = remote_addr[0], remote_addr[1]
     print "Echo Client connected from: %s:%d" % (host, port)
     while True:
         try:
             message = diesel.until_eol()
             your_message = ':'.join(['You said', message])
             diesel.send(your_message)
         except Exception, e:
             print "error:", e
             break
コード例 #42
0
ファイル: console.py プロジェクト: brucezhangf/diesel-1
 def runcode(self, ignored_codeobj):
     if self.current_source:
         sz = len(self.current_source)
         header = struct.pack('>Q', sz)
         diesel.send("%s%s" % (header, self.current_source))
         self.current_source = None
         header = diesel.receive(8)
         (sz, ) = struct.unpack('>Q', header)
         if sz:
             data = diesel.receive(sz)
             print data.rstrip()
コード例 #43
0
 def handler(self, remote_addr):
     """Runs the echo server"""
     host, port = remote_addr[0], remote_addr[1]
     print "Echo client connected from: %s:%d" % (host, port)
     while True:
         try:
             message = diesel.until_eol()
             your_message = ': '.join(['You said', message])
             diesel.send(your_message)
         except Exception as e:
             print "Exception:", e
コード例 #44
0
ファイル: console.py プロジェクト: brucezhangf/diesel-1
 def handle_command(self):
     header = diesel.receive(8)
     (sz, ) = struct.unpack('>Q', header)
     data = diesel.receive(sz)
     stdout_patch = StdoutDispatcher()
     with stdout_patch:
         self.interpreter.runsource(data)
     output = stdout_patch.contents
     outsz = len(output)
     outheader = struct.pack('>Q', outsz)
     diesel.send("%s%s" % (outheader, output))
コード例 #45
0
ファイル: websockets.py プロジェクト: yadra/diesel
    def websocket_protocol(self, req):
        """Runs the WebSocket protocol after the handshake is complete.

        Creates two `Queue` instances for incoming and outgoing messages and
        passes them to the `web_socket_handler` that was supplied to the
        `WebSocketServer` constructor.

        """
        inq = Queue()
        outq = Queue()

        if req.rfc_handshake:
            handle_frames = self.handle_rfc_6455_frames
        else:
            # Finish the non-RFC handshake
            key1 = req.headers.get('Sec-WebSocket-Key1')
            key2 = req.headers.get('Sec-WebSocket-Key2')

            # The final key can be in two places. The first is in the
            # `Request.data` attribute if diesel is *not* being proxied
            # to by a smart proxy that parsed HTTP requests. If it is being
            # proxied to, that data will not have been sent until after our
            # initial 101 Switching Protocols response, so we will need to
            # receive it here.

            if req.data:
                key3 = req.data
            else:
                evt, key3 = first(receive=8, sleep=5)
                assert evt == "receive", "timed out while finishing handshake"

            num1 = int(''.join(c for c in key1 if c in '0123456789'))
            num2 = int(''.join(c for c in key2 if c in '0123456789'))
            assert num1 % key1.count(' ') == 0
            assert num2 % key2.count(' ') == 0
            final = pack('!II8s', num1 / key1.count(' '),
                         num2 / key2.count(' '), key3)
            handshake_finish = hashlib.md5(final).digest()
            send(handshake_finish)

            handle_frames = self.handle_non_rfc_frames

        def wrap(req, inq, outq):
            self.web_socket_handler(req, inq, outq)
            outq.put(WebSocketDisconnect())

        handler_loop = fork(wrap, req, inq, outq)

        try:
            handle_frames(inq, outq)
        except ConnectionClosed:
            if handler_loop.running:
                inq.put(WebSocketDisconnect())
            raise
コード例 #46
0
    def handler(self, remote_addr):
        host, port = remote_addr[0], remote_addr[1]
        print "[*] Echo client connected from: %s:%d" % (host, port)

        while True:
            try:
                message = diesel.until_eol()
                your_message = ": ".join(["You said", message])
                diesel.send(your_message)
            except Exception, e:
                print "[-] Exception:", e
コード例 #47
0
ファイル: console.py プロジェクト: 1angxi/diesel
 def runcode(self, ignored_codeobj):
     if self.current_source:
         sz = len(self.current_source)
         header = struct.pack('>Q', sz)
         diesel.send("%s%s" % (header, self.current_source))
         self.current_source = None
         header = diesel.receive(8)
         (sz,) = struct.unpack('>Q', header)
         if sz:
             data = diesel.receive(sz)
             print data.rstrip()
コード例 #48
0
ファイル: websockets.py プロジェクト: 1angxi/diesel
    def websocket_protocol(self, req):
        """Runs the WebSocket protocol after the handshake is complete.

        Creates two `Queue` instances for incoming and outgoing messages and
        passes them to the `web_socket_handler` that was supplied to the
        `WebSocketServer` constructor.

        """
        inq = Queue()
        outq = Queue()

        if req.rfc_handshake:
            handle_frames = self.handle_rfc_6455_frames
        else:
            # Finish the non-RFC handshake
            key1 = req.headers.get('Sec-WebSocket-Key1')
            key2 = req.headers.get('Sec-WebSocket-Key2')

            # The final key can be in two places. The first is in the
            # `Request.data` attribute if diesel is *not* being proxied
            # to by a smart proxy that parsed HTTP requests. If it is being
            # proxied to, that data will not have been sent until after our
            # initial 101 Switching Protocols response, so we will need to
            # receive it here.

            if req.data:
                key3 = req.data
            else:
                evt, key3 = first(receive=8, sleep=5)
                assert evt == "receive", "timed out while finishing handshake"

            num1 = int(''.join(c for c in key1 if c in '0123456789'))
            num2 = int(''.join(c for c in key2 if c in '0123456789'))
            assert num1 % key1.count(' ') == 0
            assert num2 % key2.count(' ') == 0
            final = pack('!II8s', num1 / key1.count(' '), num2 / key2.count(' '), key3)
            handshake_finish = hashlib.md5(final).digest()
            send(handshake_finish)

            handle_frames = self.handle_non_rfc_frames

        def wrap(req, inq, outq):
            self.web_socket_handler(req, inq, outq)
            outq.put(WebSocketDisconnect())

        handler_loop = fork(wrap, req, inq, outq)

        try:
            handle_frames(inq, outq)
        except ConnectionClosed:
            if handler_loop.running:
                inq.put(WebSocketDisconnect())
            raise
    def handler(self, remote_addr):
        """ Runs the echo server """
        host, port = remote_addr[0], remote_addr[1]
        print "Echo client connected from: %s: %d" %(host, port)

        while True:
            try:
                message = diesel.until_eol()
                your_message = ':'.join(['You said', message])
                diesel.send(your_message)
            except Exception, e:
                print "Exceptioin:", e
コード例 #50
0
ファイル: udp_echo.py プロジェクト: brucezhangf/diesel-1
def echo_server():
    """The UDPService callback.

    Unlike a standard Service callback that represents a connection and takes
    the remote addr as the first function, a UDPService callback takes no
    arguments. It is responsible for receiving datagrams from the wire and
    acting upon them.

    """
    while True:
        data = receive(datagram)
        send("you said %s" % data)
コード例 #51
0
ファイル: irc.py プロジェクト: 1angxi/diesel
 def send_command(self, cmd, *args):
     if self.logged_in:
         send(":%s " % self.nick)
     acc = [cmd]
     for x, a in enumerate(args):
         ax = str(a)
         if ' ' in ax:
             assert (x == (len(args) - 1)), "no spaces except in final param"
             acc.append(':' + ax)
         else:
             acc.append(ax)
     send(' '.join(acc) + "\r\n")
コード例 #52
0
ファイル: beanstalkc.py プロジェクト: dowski/aspen
 def _interact(self, command, expected_ok, expected_err=None):
     """Send a command and wait for a response"""
     if expected_err is None:
         expected_err = []
     diesel.send(command)
     status, results = self._read_response()
     if status in expected_ok:
         return results
     elif status in expected_err:
         raise CommandFailed(command.split()[0], status, results)
     else:
         raise UnexpectedResponse(command.split()[0], status, results)
コード例 #53
0
ファイル: udp_echo.py プロジェクト: 1angxi/diesel
def echo_server():
    """The UDPService callback.

    Unlike a standard Service callback that represents a connection and takes
    the remote addr as the first function, a UDPService callback takes no
    arguments. It is responsible for receiving datagrams from the wire and
    acting upon them.

    """
    while True:
        data = receive(datagram)
        send("you said %s" % data)
コード例 #54
0
ファイル: beanstalkc.py プロジェクト: tanvir-tech/Network_Lab
 def _interact(self, command, expected_ok, expected_err=None):
     """Send a command and wait for a response"""
     if expected_err is None:
         expected_err = []
     diesel.send(command)
     status, results = self._read_response()
     if status in expected_ok:
         return results
     elif status in expected_err:
         raise CommandFailed(command.split()[0], status, results)
     else:
         raise UnexpectedResponse(command.split()[0], status, results)
コード例 #55
0
    def get(self, key):
        send("PEER-GET %s\r\n" % (key, ))
        res = until_eol().strip()
        parts = res.split(None, 1)
        cmd = parts[0].upper()

        if cmd == 'PEER-GET-MISSING':
            return None
        else:
            assert cmd == 'PEER-GET-VALUE'
            rest = parts[1]
            value = StoredValue.from_peer_wire(rest)
            return value
    def handler(self, remoteAddr):
        host = remoteAddr[0]
        port = remoteAddr[1]
        print 'Echo client connected %s:%d' % (host, port)

        while True:
            try:
                message = diesel.until_eol()
                yourMsg = 'You said: ' + message
                diesel.send(yourMsg)
            except Exception as e:
                print '[-]', e
                break  #书中没有这个break,在断开客户端连接的时候会导致服务端进入死循环。加上之后客户端断开连接,服务端并没有结束,而是等待下一个连接
コード例 #57
0
 def send_command(self, cmd, *args):
     if self.logged_in:
         send(":%s " % self.nick)
     acc = [cmd]
     for x, a in enumerate(args):
         ax = str(a)
         if ' ' in ax:
             assert (x == (len(args) -
                           1)), "no spaces except in final param"
             acc.append(':' + ax)
         else:
             acc.append(ax)
     send(' '.join(acc) + "\r\n")
コード例 #58
0
    def wait(self, timeout, clocks):
        parts = chain([timeout], 
                *clocks.iteritems())
        rest = ' '.join(map(str, parts))
        send("BLOCKON " + rest + "\r\n")

        response = until_eol().strip()
        parts = response.split()
        result, rest = parts[0].upper(), parts[1:]
        if result == "BLOCKON-DONE":
            return ConvoyWaitDone(rest[0])
        assert result == "BLOCKON-TIMEOUT"
        return ConvoyWaitTimeout()
コード例 #59
0
def handle_con(unused):
    while True:
        line = until_eol()
        if line == "":
            break

        parts = line.split()
        cmd = parts[0]

        if cmd == "get":
            key = parts[1]

            try:
                val = CACHE[key]
                send("VALUE %s 0 %d\r\n" % (key, len(val)))
                send(val + "\r\n")
            except KeyError:
                pass
            send("END\r\n")

        elif cmd == "set":
            key = parts[1]
            #exp = parts[2]
            #flags = parts[3]
            length = int(parts[4])
            val = receive(length + 2)[:length]
            CACHE[key] = val

            send("STORED\r\n")