Pipe Bi-directional communication C# server and Python server - python

I have been trying to establish a bidirectional communication between a C# local server and a Python local server. What I wish to achieve:
Send a string from C# to Python
Compute the string in Python and send back the result to C#
Do this as the Python instance is continuously running as I require a time-expensive package import
My C# code for the pipe creation:
PipeSecurity pSecure = new PipeSecurity();
pSecure.SetAccessRule(new PipeAccessRule("Everyone", PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
using (NamedPipeServerStream pipeClient = new NamedPipeServerStream("NLP", PipeDirection.InOut)) {
pipeClient.SetAccessControl(pSecure);
Console.WriteLine("Attempting to connect to pipe...");
pipeClient.WaitForConnection();
Console.WriteLine("Is pipe connected? " + pipeClient.IsConnected);
Console.WriteLine("Connected to pipe");
using (StreamReader sr = new StreamReader(pipeClient)) {
using (StreamWriter sw = new StreamWriter(pipeClient)) {
// initialize python server
sw.WriteLine("true; nothing; false");
foreach (AlignedVariation variation in _nonConformancePatterns.Keys) {
foreach (NonConformancePattern pattern in _nonConformancePatterns[variation]) {
// create argument to be parsed by Python
string pythonParameter = "false; ";
foreach (string activityName in pattern.EventsSkippedNames()) {
pythonParameter += activityName + "|";
}
pythonParameter += "; false";
sw.WriteLine(pythonParameter);
pattern.SetLingvisticTag(sr.ReadLine()!);
}
}
// Close connection to Python server
sw.WriteLine("false; nothing; true");
}
}
}
My Python code for the pipe interpretation:
class PipeServer():
def __init__(self, pipeName):
self.pipe = win32pipe.CreateNamedPipe(r'\\.\pipe\NLP', win32pipe.PIPE_ACCESS_DUPLEX,
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE |
win32pipe.PIPE_WAIT, 1, 65536, 65536, 0, None)
#Carefull, this blocks until a connection is established
def connect(self):
win32pipe.ConnectNamedPipe(self.pipe, None)
#Message without tailing '\n'
def write(self, message):
win32file.WriteFile(self.pipe, message.encode()+b'\n')
def read(self):
return win32file.ReadFile(self.pipe)
def close(self):
win32file.CloseHandle(self.pipe)
I could not find a better solution that does not imply the creation of a file in Python for when reading from a pipe. Furthermore, I have read on the documentation of the pywin32 package but it mentions when reading from a pipe the only solution is to use a PyHandle class to handle the reading but I have seen solutions doing this only from reading a file.
How can I change the Python code to be able to access the pipeline? At the moment whenever I want to connect the Python instance to the pipe created in C# I get the error that permission is denied.
Also, is the code for sending and receiving messages correct in C#?

Related

Keeping track of multiple clients on grpc server

I am trying to create a grpc python server that can keep track of all clients connected.
I am referencing a talk/demo that Ray Tsang did where he kept a collection of StreamObservers and just iterated through them to send to all the clients. Here is a video of that for reference.
Now my question is how do you get a StreamObserver in python? I only see self, request and context as being available to me in the definition.
This is my first python project so there might be something obvious I am missing here.
Here is my proto, its basically the sample proto
syntax = "proto3";
package hellostreamingworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
rpc SayHelloAgain (HelloRequest) returns (stream HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
If I have understand you need to create a class ClientInterceptor that extends grpc.UnaryUnaryClientInterceptor(https://grpc.io/grpc/python/grpc.html?highlight=unaryunaryclientinterceptor#grpc.UnaryUnaryClientInterceptor)
and then assign it with the intercept_channel method in this way
self.channel = grpc.insecure_channel(address, options)
self.channel = grpc.intercept_channel(
self.channel,
ClientInterceptor()
)
You can use the intercept_unary_unary method for receive infos about the various clients.
If you want to have infos server side extends the ServerInterceptor(https://grpc.io/grpc/python/grpc.html?highlight=serverinterceptor#grpc.ServerInterceptor) and assign it on the server init
self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=1000),
options=(('grpc.max_send_message_length', 1000 * 1024 * 1024,),
('grpc.max_receive_message_length', 1000 * 1024 * 1024,),
),
interceptors=(MyServerInterceptor())
)
And the use the intercept_service method for receive infos.
What you probably need is a bidirectional stream service. That is both the client and server are continuously sending data over. In this case gRPC + python will keep track of your client!
Check out this example code and corresponding explanation.

Why does ZeroMQ req-rep give me an empty response between C++ and Python?

I'm trying to bridge between a Python client and a C++ based server using ZeroMQ but I'm struggling to get the correct response returned from the server back to the client.
The Python client looks like this:
import zmq
import time
# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect ("tcp://127.0.0.1:5563")
requestId = 0
while True:
request = "Message %d"%requestId
print ("Sending '%s'.. "%request)
socket.send_string(request)
response = socket.recv()
print ("Response:",response)
requestId += 1
time.sleep(1)
The C++ server looks like this:
zmq::context_t* context = new zmq::context_t();
zmq::socket_t* publisher = new zmq::socket_t(*context, ZMQ_REP);
unsigned int port = 5563;
std::ostringstream bindDest;
bindDest << "tcp://*:" << port;
publisher->bind(bindDest.str());
zmq::message_t request(0);
bool notInterrupted = true;
while (notInterrupted)
{
// Wait for a new request to act upon.
publisher->recv(&request, 0);
// Turn the incoming message into a string
char* requestDataBuffer = static_cast<char*>(request.data());
std::string requestStr(requestDataBuffer, request.size());
printf("Got request: %s\n", requestStr.c_str());
// Call the delegate to get a response we can pass back.
std::string responseString = requestStr + " response";
printf("Responding with: %s\n", responseString.c_str());
// Turn the response string into a message.
zmq::message_t responseMsg(responseString.size());
char* responseDataBuffer = static_cast<char*>(responseMsg.data());
const int OffsetToCopyFrom = 0;
responseString.copy(responseDataBuffer, responseString.size(), OffsetToCopyFrom);
// Pass back our response.
publisher->send(&responseMsg, 0);
}
When I run this, the client reports:
Sending 'Message 0'..
The server reports:
Got request: Message 0
Responding with: Message 0 response
But the python receives a blank message:
Response: b''
If I replace the C++ version, with a Python implementation as follows it works as expected:
import zmq
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind ("tcp://127.0.0.1:5563")
replyId = 0
while True:
request = socket.recv_string()
print("Received: "+request)
response = request+" response %d"%replyId
print ("Sending response: "+response)
socket.send_string(response)
replyId += 1
What am I doing wrong in the C++ version?
Update - Checked versions...
Reading around other people's problems, one thing people have suggested is that different versions sometimes cause issues. I double-checked and Python is using v4.1.6 whereas C++ is on v4.0.4.
I'm using the C++ pre-built libraries from here: http://zeromq.org/distro:microsoft-windows so I guess that could be the cause? I made a similar setup to publish from C++ to Python and that worked fine but perhaps something in the Req-Rep area has changed.
Update2 - It doesn't appear to be the versions...
After a lot of hacking around I finally managed to get v4.1.6 to build for the C++ version of the server and I still get the same empty message.
You need to send the buffer, not the pointer.
publisher->send(responseMsg, 0);
did the trick when I tested your code.
Hope this helps,
Hannu

Golang - Sending binary encoded data via TLS Socket

I'm new to Golang and this kind of more lowlevel stuff, so maybe i'm just thinking in the wrong direction.
My project is a small golang monitoring client which sends some ping-message (current date) over a encrypted TLS connection (this is just for learning purpose).
The python server and client are working flawless for now.
The (python) server & client are packing the data like this:
[...]
def _send_msg(self, msg):
msg = struct.pack('>I', len(msg)) + bytes(msg, 'UTF-8')
self.client.send(msg)
def _recv_msg(self):
raw_msglen = self.client.recv(4)
if not raw_msglen:
return ""
msglen = struct.unpack('>I', raw_msglen)[0]
return self._recvall(msglen)
[...]
On the Go-Side I pack the data like this:
type packet struct {
a uint32
b []byte
}
func pack(d string) []byte {
buf := bytes.Buffer{}
u := len([]byte(d))
p := packet{
a: uint32(u),
b: []byte(d),
}
err := binary.Write(&buf, binary.BigEndian, p.a) // struct.unpack('>I' [...] in python
if err != nil {
fmt.Println(err)
}
buf.Write(p.b) // Append bytes to buffer
return buf.Bytes()
}
reader := bufio.NewReader(tlsConn) // Socket reader
[..]
// Writing binary data
tlsConn.Write(pack("login test:test")) // Works so far
// Reading response (from python server)
var p uint32 // packet size
binary.Read(reader, binary.BigEndian, &p) // Read packet length (4 bytes uint32)
buf1 := make([]byte, int(p))
reader.Read(buf1)
fmt.Println(string(buf1)) // Print response - this also works
// Send some ping
// This code part also get's passed, but the python server doesn't
// recive any message
c.Write(pack("ping 0000-00-00 00:00:00"))
binary.Read(reader, binary.BigEndian, &p)
buf1 = make([]byte, int(p))
fmt.Println(string(buf1)) // Here i don't get an response
Here is the golang client-side test code which I'm using: http://pastebin.com/dr1mJZ9Y
and the corresponding terminal output: http://pastebin.com/mrctJPs5
The strange thing is, that the login message (including the server response) all get sent (and received) correctly but the second time when i try to send a ping like:
tlsConn.Write(pack("ping 0000-00-00 00:00:00"))
The message seems not to reach the server and no response message gets send back to the client.
Is there an error with the binary encoding of the packet-data and the length prefixing?
I know the code looks a bit messy and not very go-idiomatic, sorry for that.
Thank you again in advance for your help!
Software dependencies / environment:
Python 3.4
Golang 1.5.3 (amd64 Linux)
No 3rd-Party libs

Telnet send command and then read response

This shouldn't be that complicated, but it seems that both the Ruby and Python Telnet libs have awkward APIs. Can anyone show me how to write a command to a Telnet host and then read the response into a string for some processing?
In my case "SEND" with a newline retrieves some temperature data on a device.
With Python I tried:
tn.write(b"SEND" + b"\r")
str = tn.read_eager()
which returns nothing.
In Ruby I tried:
tn.puts("SEND")
which should return something as well, the only thing I've gotten to work is:
tn.cmd("SEND") { |c| print c }
which you can't do much with c.
Am I missing something here? I was expecting something like the Socket library in Ruby with some code like:
s = TCPSocket.new 'localhost', 2000
while line = s.gets # Read lines from socket
puts line # and print them
end
I found out that if you don't supply a block to the cmd method, it will give you back the response (assuming the telnet is not asking you for anything else). You can send the commands all at once (but get all of the responses bundled together) or do multiple calls, but you would have to do nested block callbacks (I was not able to do it otherwise).
require 'net/telnet'
class Client
# Fetch weather forecast for NYC.
#
# #return [String]
def response
fetch_all_in_one_response
# fetch_multiple_responses
ensure
disconnect
end
private
# Do all the commands at once and return everything on one go.
#
# #return [String]
def fetch_all_in_one_response
client.cmd("\nNYC\nX\n")
end
# Do multiple calls to retrieve the final forecast.
#
# #return [String]
def fetch_multiple_responses
client.cmd("\r") do
client.cmd("NYC\r") do
client.cmd("X\r") do |forecast|
return forecast
end
end
end
end
# Connect to remote server.
#
# #return [Net::Telnet]
def client
#client ||= Net::Telnet.new(
'Host' => 'rainmaker.wunderground.com',
'Timeout' => false,
'Output_log' => File.open('output.log', 'w')
)
end
# Close connection to the remote server.
def disconnect
client.close
end
end
forecast = Client.new.response
puts forecast

using suds to pull data points through wsdl on Smartserver 2.0

Im writing a console app that pulls smartserver data points using the Read function provided by the wsdl. Through suds I can successfully connect to the smartserver and print the client wsdl which gives me a list of methods and types. How can I use these methods through suds to pull data? Ive tried print client.service.List() which according to the programmer documentation of this server should give me data points, but this gives me a urlopen error [Errno 13] Permission denied. The manual provides example codes that just uses SOAP to pull the data but since I'm using suds alot of this code is streamlined and I only have to do client.service.somemethod(parameter) I have attached my code so far and the list of methods I get when I print client.
Thank you very much.
import suds
from suds.client import Client
from suds.transport.http import HttpAuthenticated
url = "http://example/WSDL/v4.0/foo.WSDL"
client = Client(url, username='foo', password='bar')
myheaders = dict(userid='foo', passwd='bar')
client.set_options(soapheaders=myheaders)
name = client.factory.create('ns0:E_xSelect')
print name
name['xSelect'] = """//Item[UCPTpointName = "Net/MB485/MAIN POWER/Fb/PowerSum"]"""
print client.service.Read(name)
what I get in console
Ports (1):
(iLON100httpPort)
Methods (8):
Clear(ns0:Item_Coll iLonItem, )
Delete(ns0:Item_Coll iLonItem, )
Get(ns0:Item_Coll iLonItem, )
InvokeCmd(ns0:Item_Coll iLonItem, )
List(ns0:E_xSelect iLonItem, )
Read(ns0:Item_Coll iLonItem, )
Set(ns0:Item_CfgColl iLonItem, )
Write(ns0:Item_DataColl iLonItem, )
Example code in the documentation to give you an idea
static void Main(string[] args)
{
iLON_SoapCalls.BindClientToSmartServer(); iLON_SmartServer.iLON100portTypeClient SmartServer = iLON_SoapCalls._iLON;
// -------------- READING A DATA POINT VALUE --------------
try
{
// instantiate the member object
iLON_SmartServer.Item_Coll itemColl = new iLON_SmartServer.Item_Coll(); itemColl.Item = new iLON_SmartServer.Item[1];
itemColl.Item[0] = new iLON_SmartServer.Dp_Data();
// set the DP name
itemColl.Item[0].UCPTname = "Net/LON/iLON App/Digital Output 1/nviClaValue_1";
// set maxAge to get the updated DP value in case it has been cached for more than 10 // seconds on the Data Server (see section 4.3.4.1 for more information) ((iLON_SmartServer.Dp_Data)(itemColl.Item[0])).UCPTmaxAge = 10; ((iLON_SmartServer.Dp_Data)(itemColl.Item[0])).UCPTmaxAgeSpecified = true;
//call the Read Function
iLON_SmartServer.Item_DataColl dataColl = SmartServer.Read(itemColl);
if (dataColl.Item == null)
{
// sanity check. this should not happen
Console.Out.WriteLine("No items were returned");
}
else if (dataColl.Item[0].fault != null)
{
// error
Console.Out.WriteLine("An error occurred. Fault code = " +
dataColl.Item[0].fault.faultcode +
". Fault text = %s." +
dataColl.Item[0].fault.faultstring);
}
else
{
// success
Console.Out.WriteLine("Read is successful");
Console.Out.WriteLine(((iLON_SmartServer.Dp_Data)dataColl.Item[0]).UCPTname + " = " + ((iLON_SmartServer.Dp_Data)dataColl.Item[0]).UCPTvalue[0].Value + "\n");
}
I figured out the problem. You have to open up the wsdl you are accessing through soap in an xml format and read the section named wsdl services that specify a location. Define that location in the client constructor to successfully communicate with the server. For some reason suds wasn't seeing this location in the wsdl file.

Categories