50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
import win32pipe
|
|
import win32file
|
|
import struct
|
|
|
|
|
|
class HostClient:
|
|
def __init__(self):
|
|
self.pipe_name = r"\\.\pipe\BotExtPipe"
|
|
self.pipe = None
|
|
|
|
def connect(self):
|
|
self.pipe = win32file.CreateFile(
|
|
self.pipe_name,
|
|
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
|
|
0,
|
|
None,
|
|
win32file.OPEN_EXISTING,
|
|
0,
|
|
None,
|
|
)
|
|
|
|
def write(self, data):
|
|
if not self.pipe:
|
|
self.connect()
|
|
length = len(data)
|
|
length_bytes = struct.pack("I", length)
|
|
win32file.WriteFile(self.pipe, length_bytes)
|
|
win32file.WriteFile(self.pipe, data)
|
|
|
|
def read(self):
|
|
if not self.pipe:
|
|
self.connect()
|
|
length_bytes = win32file.ReadFile(self.pipe, 4)[1]
|
|
length = struct.unpack("I", length_bytes)[0]
|
|
data = win32file.ReadFile(self.pipe, length)[1]
|
|
return data
|
|
|
|
def close(self):
|
|
if self.pipe:
|
|
win32file.CloseHandle(self.pipe)
|
|
self.pipe = None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
client = HostClient()
|
|
client.connect()
|
|
client.write(b"Hello, World!")
|
|
print(client.read())
|
|
client.close()
|