-
Notifications
You must be signed in to change notification settings - Fork 15
/
Pipes_Client.py
55 lines (48 loc) · 1.6 KB
/
Pipes_Client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import struct
import time
import win32pipe
import win32file
import pywintypes
# IPC parameters
PIPE_NAME = r'\\.\pipe\simple-ipc-pipe'
ENCODING = 'ascii'
stock_list=['NSE:TATAMOTORS,C','NSE:OFSS,C','NSE:BPCL,P']
#while True:
pipe = win32file.CreateFile(
PIPE_NAME,
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
0,
None,
win32file.OPEN_EXISTING,
0,
None
)
''' try:
res = win32pipe.SetNamedPipeHandleState(pipe, win32pipe.PIPE_READMODE_MESSAGE, None, None)
if res == 0:
print(f"SetNamedPipeHandleState Return Code: {res}") # if function fails, the return value will be zero
while True:
# Read the data from the named Pipe
resp = win32file.ReadFile(pipe, 65536)
print(f"Data Received: {resp}") # if function fails, the return value will be zero
except pywintypes.error as e:
if e.args[0] == 2: # ERROR_FILE_NOT_FOUND
print("No Named Pipe")
elif e.args[0] == 109: # ERROR_BROKEN_PIPE
print("Named Pipe is broken")
#break
#with open(PIPE_NAME , 'rb+', buffering=0) as f: '''
for index in range(3):
data = stock_list[index].encode(ENCODING)
data_len = struct.pack('I', len(data))
pipe.write(data_len)
pipe.write(data)
pipe.seek(0) # Necessary
time.sleep(5)
received_len = struct.unpack('I', pipe.read(4))[0]
received_data = pipe.read(received_len).decode(ENCODING)
pipe.seek(0) # Also necessary
print(f"Received data: {repr(received_data)}")
time.sleep(5)
#print(win32pipe.CallNamedPipe(r"\\.\pipe\simple-ipc-pipe", 'NSE:TATAMOTORS,C'.encode(ENCODING), 64, 0))
#print('5')