Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Python Client on Linux Using a .NET Core Named Pipe (via unix domain sockets)
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 12 19:06:38 2017
@author: agoswami
"""
#socket_echo_client_uds.py
import socket
import sys
import struct
# Create a UDS socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = '/tmp/CoreFxPipe_testpipe'
print('connecting to {}'.format(server_address))
try:
sock.connect(server_address)
except socket.error as msg:
print(msg)
sys.exit(1)
try:
while True:
amount_expected = struct.unpack('I', sock.recv(4))[0]
print("amount_expected :", amount_expected)
message = sock.recv(amount_expected)
print("Received message : ", message.decode()) # making it a string
# Send data
message_rev = message[::-1].decode() # making it a string
print("Sent message (reversed) : ", message_rev)
sock.sendall(struct.pack('I', len(message_rev)) + message_rev.encode('utf-8'))
finally:
print('closing socket')
sock.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment