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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def writePipe() :
    print("pipe server")
    count = 0
    pipe = win32pipe.CreateNamedPipe(
        r'\\.\pipe\Foo',  # name of pipe
        win32pipe.PIPE_ACCESS_DUPLEX, # openmode
        win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT, #pipe mode
        16553665536# nMaxinstances , nOutBufferSize , nInbufferSize
        0# nDrfaultTimeOut
        None) # pysecurity attributes
    try:
        print("waiting for client")
        win32pipe.ConnectNamedPipe(pipe, # handle
                                   None) # overlapped
        print("got client")
 
        print(f"writing message {count}")
            # convert to bytes
        
        data = yHdr_s("hello pipe....!!")
        
        print(f"data to send : {data}")
        
        win32file.WriteFile(pipe, data) 
        #win32file.WriteFile(pipe, b'hello pipe...!') 
        time.sleep(1)
 
        print("finished now")
    finally:
        win32file.CloseHandle(pipe)
 
def readPipe() :
    print("pipe client")
    quit = False
 
    while not quit:
        try:
            handle = win32file.CreateFile(
                r'\\.\pipe\Foo'# filename
                win32file.GENERIC_READ | win32file.GENERIC_WRITE, # win32con.GENERIC_READ | win32con.GENERIC_WRITE,
                0#win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
                None, # win32security.SECURITY_ATTRIBUTES(),
                win32file.OPEN_EXISTING, #win32con.OPEN_EXISTING,
                0# win32con.FILE_FLAG_OVERLAPPED,
                None # 0
            )
            res = win32pipe.SetNamedPipeHandleState(
                    handle, #HANDLE
                    win32pipe.PIPE_READMODE_MESSAGE, #PIPE_READMODE_BYTE OR PIPE_READMODE_MESSAGE
                    None,  # PIPE_WAIT OR PIPE_NOWAIT
                    None)  #NULL
            if res == 0:
                print(f"SetNamedPipeHandleState return code: {res}")
            while True:
                resp = win32file.ReadFile(handle, 64*1024)
                print(f"message: {resp}")
                print(type(resp[1]))
        except pywintypes.error as e:
            if e.args[0== 2:
                print("no pipe, trying again in a sec")
                time.sleep(1)
            elif e.args[0== 109:
                print("broken pipe, bye bye")
                quit = True
 
 
if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("need s or c as argument")
    elif sys.argv[1== "s":
        writePipe()
    elif sys.argv[1== "c":
        readPipe()
    else:
        print(f"no can do: {sys.argv[1]}")
cs


+ Recent posts