-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_server.py
53 lines (38 loc) · 1.24 KB
/
pipe_server.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
import win32pipe, win32file
bufferSize = 4096
def CreateNamedPipe(name):
pipe = win32pipe.CreateNamedPipe(
'\\\\.\\pipe\\' + name,
win32pipe.PIPE_ACCESS_DUPLEX,
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT,
1, 65536, 65536,
0,
None)
return pipe
def Open(pipe_handle):
win32pipe.ConnectNamedPipe(pipe_handle, None)
def WritePipe(pipe, data):
win32file.WriteFile(pipe, data)
def ReadPipe(pipe):
win32file.SetFilePointer(pipe, 0, win32file.FILE_BEGIN)
result, data = win32file.ReadFile(pipe, bufferSize, None)
buf = data
while len(data) == bufferSize:
result, data = win32file.ReadFile(pipe, bufferSize, None)
buf += data
return buf
def Close(pipe):
win32file.CloseHandle(pipe)
pipe = CreateNamedPipe("Foo")
# this will wait for a client
print("Waiting for client!")
Open(pipe)
print("Received client!")
WritePipe(pipe, b'hello im am the server with lot of data')
data = ReadPipe(pipe)
print(data)
input("Press enter to close pipe")
Close(pipe)
#reference
# https://stackoverflow.com/questions/48542644/python-and-windows-named-pipes
# https://stackoverflow.com/questions/29910861/python-read-file-using-win32file-readfile