Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100546

asyncio for UNIX socket

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Pavlos Parissis <pavlos.parissis@gmail.com>
Newsgroups comp.lang.python
Subject asyncio for UNIX socket
Date Thu, 17 Dec 2015 02:16:11 +0100
Lines 72
Message-ID <mailman.27.1450314981.30845.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="HjL0aPeCdOC7AfLSAbUbUEn7DtHJuCiKi"
X-Trace news.uni-berlin.de eZNsXOT0uRlfg0E6x9u06QtQnNCU07ab6cmwuwYMPqCQ==
Return-Path <pavlos.parissis@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'data):': 0.07; 'socket': 0.07; "'w')": 0.09; 'cmd': 0.09; 'fp:': 0.09; 'self.data': 0.09; 'def': 0.13; 'filename:fname piece:signature': 0.16; 'received:192.168.0.103': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'self.cmd': 0.16; 'skip:l 30': 0.18; 'skip:u 30': 0.18; 'skip:{ 20': 0.18; 'sends': 0.22; 'file.': 0.22; 'trying': 0.22; 'thanks,': 0.24; 'import': 0.24; 'unix': 0.24; 'header:User-Agent:1': 0.26; 'peer': 0.29; 'saves': 0.30; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'message- id:@gmail.com': 0.34; "skip:' 20": 0.34; 'file': 0.34; 'server': 0.34; 'received:google.com': 0.35; 'sent:': 0.35; 'received:74.125.82': 0.35; 'skip:{ 10': 0.36; 'to:addr:python- list': 0.36; 'client': 0.37; 'doing': 0.38; 'skip:o 20': 0.38; 'hi,': 0.38; 'data': 0.39; 'sure': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'skip:u 10': 0.61
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type; bh=Oyi7R+X/KIAKPxUttAbVO8SwFoGzdbwuSjJYGuImUJA=; b=Z0sam+YPTKP7d40LJiJs2O7WAHzi3QFlovJCVo9b/xhBgzpEujCbt49STtgSMtGkHs HL37iAaQLCAEG5P6AfWrYYM0VdxpyoGUDzQmkhy8uhexyJjqH8yow+9VlWYiMtG4DiDv Tk7Hy1S2PvvkWti6GTW2j5eXmUlHnpk6/VAZlQSy6/MHNriZzsgBB8Xs1/oOq2P4Q3xH k9QxNeaeoQU/+laejniRiZmEIep+BFeQbhg06SDb4iD7dGBNWKzGCfaq+RQVHqgoB/eD VHa49L/D2LY4ONIqBpl/men08HOzNrIKb1TJd2Xs0oGcN6NOh2giv0NNDEZFAPfb9vw6 0ABA==
X-Received by 10.194.91.234 with SMTP id ch10mr58722359wjb.69.1450314973983; Wed, 16 Dec 2015 17:16:13 -0800 (PST)
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:100546

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Hi,
I am trying to write UNIX socket client which sends 1 cmd
and saves the received data to a file.
Based on what I found on documentation I came up with::

import asyncio
class UnixProtocol(asyncio.Protocol):
    def __init__(self, loop):
        self.cmd = 'show stat\n'
        self.loop = loop
        self.data = []

    def connection_made(self, transport):
        transport.write(self.cmd.encode())
        print('Data sent: {!r}'.format(self.message))

    def data_received(self, data):
        print('Data received: {!r}'.format(data))
        self.data.append(data.decode())

    def connection_lost(self, exc):
        print('The server closed the connection')
        print('Stop the event loop')
        if self.data:
            with open('/tmp/somedata', 'w') as fp:
                fp.writelines(self.data)
        self.loop.stop()

loop = asyncio.get_event_loop()
s_file = '/run/haproxy/admin1.sock'
coro = loop.create_unix_connection(lambda: UnixProtocol(loop), s_file)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()

I am not sure if the above is the proper way to do the job.
Am I doing the right thing to write the data to the file when peer
closes the connection?

Thanks,
Pavlos

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

asyncio for UNIX socket Pavlos Parissis <pavlos.parissis@gmail.com> - 2015-12-17 02:16 +0100

csiph-web