Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100546
| From | Pavlos Parissis <pavlos.parissis@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | asyncio for UNIX socket |
| Date | 2015-12-17 02:16 +0100 |
| Message-ID | <mailman.27.1450314981.30845.python-list@python.org> (permalink) |
[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
asyncio for UNIX socket Pavlos Parissis <pavlos.parissis@gmail.com> - 2015-12-17 02:16 +0100
csiph-web