def pipe_server(name): pipe = win32pipe.CreateNamedPipe( name, win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE , 1, 65536, 65536, 0, None) win32pipe.ConnectNamedPipe(pipe, None)
def __init__(self): print("__init__") self.pipe_name = "KEG_Pipe_Dairy" ## 名前付きパイプの作成 self.pipe = win32pipe.CreateNamedPipe( r'//./pipe/' + self.pipe_name, win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_READMODE_BYTE | win32pipe.PIPE_WAIT, 1, 256, 256, 0, None) self.db = DBConnect()
def send_pipe_message(message, address): pipe = '\\\\.\\pipe\\%s' % (address) p = win32pipe.CreateNamedPipe( pipe, win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 300, None) win32pipe.ConnectNamedPipe(p, None) data = compress_object(message) win32file.WriteFile(p, data) win32file.CloseHandle(p)
def run(self): # REJECT doesn't do anything under XP, but XP is gone anyway PIPE_REJECT_REMOTE_CLIENTS = 0x00000008 FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000 buffer_ = 4096 timeout_ms = 50 try: handle = win32pipe.CreateNamedPipe( self._filename, win32pipe.PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE, (win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_READMODE_BYTE | win32pipe.PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS), win32pipe.PIPE_UNLIMITED_INSTANCES, buffer_, buffer_, timeout_ms, None) except pywintypes.error: # due to FILE_FLAG_FIRST_PIPE_INSTANCE and not the first instance self._stopped = True self._event.set() return if handle == win32file.INVALID_HANDLE_VALUE: self._stopped = True self._event.set() return self._event.set() while 1: data = bytearray() try: win32pipe.ConnectNamedPipe(handle) while 1: try: code, message = win32file.ReadFile( handle, buffer_, None) except pywintypes.error: break data += message win32pipe.DisconnectNamedPipe(handle) except pywintypes.error: # better not loop forever.. break finally: if self._stopped: break if data: self._process(bytes(data)) try: win32file.CloseHandle(handle) except pywintypes.error: pass
def SvcDoRun(self): # We create our named pipe. pipeName = "\\\\.\\pipe\\PyPipeService" openMode = win32pipe.PIPE_ACCESS_DUPLEX | win32file.FILE_FLAG_OVERLAPPED pipeMode = win32pipe.PIPE_TYPE_MESSAGE # When running as a service, we must use special security for the pipe sa = pywintypes.SECURITY_ATTRIBUTES() # Say we do have a DACL, and it is empty # (ie, allow full access!) sa.SetSecurityDescriptorDacl(1, None, 0) pipeHandle = win32pipe.CreateNamedPipe( pipeName, openMode, pipeMode, win32pipe.PIPE_UNLIMITED_INSTANCES, 0, 0, 6000, # default buffers, and 6 second timeout. sa) # Loop accepting and processing connections while 1: try: hr = win32pipe.ConnectNamedPipe(pipeHandle, self.overlapped) except error, details: print "Error connecting pipe!", details pipeHandle.Close() break if hr == winerror.ERROR_PIPE_CONNECTED: # Client is fast, and already connected - signal event win32event.SetEvent(self.overlapped.hEvent) # Wait for either a connection, or a service stop request. timeout = win32event.INFINITE waitHandles = self.hWaitStop, self.overlapped.hEvent rc = win32event.WaitForMultipleObjects(waitHandles, 0, timeout) if rc == win32event.WAIT_OBJECT_0: # Stop event break else: # Pipe event - read the data, and write it back. # (We only handle a max of 255 characters for this sample) try: hr, data = win32file.ReadFile(pipeHandle, 256) win32file.WriteFile(pipeHandle, "You sent me:" + data) # And disconnect from the client. win32pipe.DisconnectNamedPipe(pipeHandle) except win32file.error: # Client disconnected without sending data # or before reading the response. # Thats OK - just get the next connection continue
def __init__(self): self.handle = win32pipe.CreateNamedPipe(PIPE, win32pipe.PIPE_ACCESS_DUPLEX| win32file.FILE_FLAG_OVERLAPPED, win32pipe.PIPE_TYPE_MESSAGE| win32pipe.PIPE_READMODE_MESSAGE| win32pipe.PIPE_WAIT, 1, BUFSIZE, BUFSIZE, win32pipe.NMPWAIT_WAIT_FOREVER, None) win32pipe.ConnectNamedPipe(self.handle, None)
def create_pipe(self): self.pipe = win32pipe.CreateNamedPipe( self.pipe_name, # r'\\.\pipe\nt_snd_42', win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 0, None)
def create_connection(self, **kwargs): pipe_name = 'wexpect_{}'.format(self.console_pid) pipe_full_path = r'\\.\pipe\{}'.format(pipe_name) logger.info('Start pipe server: %s', pipe_full_path) self.pipe = win32pipe.CreateNamedPipe( pipe_full_path, win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 0, None) logger.info("waiting for client") win32pipe.ConnectNamedPipe(self.pipe, None) logger.info('got client')
def __init__(self): if sys.platform == 'win32': self.is_win32 = True self.pipe_filename = self.PIPE_FILENAME_WIN32 if not os.path.exists(self.pipe_filename): if self.is_win32: self.pipe = win32pipe.CreateNamedPipe(self.pipe_filename, win32pipe.PIPE_ACCESS_OUTBOUND, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_NOWAIT, 1, 65536, 65536, 30000, None) else: os.mkfifo(self.pipe_filename)
def func_read(): PIPE_NAME = r'\\.\pipe\test_pipe2' PIPE_BUFFER_SIZE = 1 named_pipe = win32pipe.CreateNamedPipe(PIPE_NAME, win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT | win32pipe.PIPE_READMODE_MESSAGE, win32pipe.PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, 500, None) win32pipe.ConnectNamedPipe(named_pipe, None) while True: res = win32file.ReadFile(named_pipe, 1)
def __init__(self, service = None): if service != None: self.logger = service.logger else: self.logger = EmptyLogger() self.pipe = win32pipe.CreateNamedPipe(r'\\.\pipe\airpods-service', win32pipe.PIPE_ACCESS_OUTBOUND, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_NOWAIT, 1, 65536, 0, 0, None) self.hasClient = False
def startServer(self): default_buffer_size = 1100000 try: self._pipe_handler = win32pipe.CreateNamedPipe( self._name, win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT, 1, default_buffer_size, default_buffer_size, 0, None) except WindowsError as e: if e.winerror != win32pipe.ERROR_PIPE_BUSY: self._pipe_handler = None raise
def __init__(self): self.pipe_h = win32pipe.CreateNamedPipe( WIRESHARK_PIPE, win32pipe.PIPE_ACCESS_OUTBOUND, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 300, None) logging.info( "Waiting for wireshark to connect to pipe '%(WIRESHARK_PIPE)s'" % globals()) win32pipe.ConnectNamedPipe(self.pipe_h, None) win32file.WriteFile(self.pipe_h, INITIAL_PCAP_HEADER)
def pipe_server(): logging.info('pipe server start ...') count = 0 pipe = win32pipe.CreateNamedPipe( r'\\.\pipe\testpipe', # pipe name \\.\pipe\<pipename> win32pipe.PIPE_ACCESS_DUPLEX, # open mode win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT, # pipe mode 1, # max Instance for the pipe 65536, # Out buffer size eg: 16bit 65536, # In buffer size eg: 16bit 0, # default time out None) # Security attribute try: logging.info('Waiting response from client') win32pipe.ConnectNamedPipe(pipe, None) logging.info('Received respond from client ') # while count < 10: # # convert to bytes # some_data = str(count).encode() # logging.info(f'Generate data {some_data}') # p = subprocess.Popen([sys.executable, 'print_input.py'], # stdin=subprocess.PIPE, # stdout=subprocess.PIPE, # stderr=subprocess.PIPE) # out, err = p.communicate(input=some_data) # logging.info(f'writing message {out}') # win32file.WriteFile( # return error code # pipe, # filehandle # out) # string # time.sleep(1) # count += 1 # logging.info('finihsed') loop = True while loop: hr, msg = win32file.ReadFile(pipe, 64 * 1024) if not hr: logging.info(f'Received input message: {msg}') p = subprocess.Popen([sys.executable, 'print_input.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate(input=msg) logging.info(f'writing message {out}') win32file.WriteFile(pipe, out) logging.info(f'Done sent message') time.sleep(1) msg = msg.decode() if 'stop' in msg: logging.info(f'Stopping') loop = False finally: win32file.CloseHandle(pipe)
def __init__(self, name, link_type_tap): self.pipe = win32pipe.CreateNamedPipe( name, win32pipe.PIPE_ACCESS_OUTBOUND, win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 1000, None, ) self.link_type_tap = link_type_tap
def func_write(): PIPE_NAME = r'\\.\pipe\test_pipe' PIPE_BUFFER_SIZE = 1 named_pipe = win32pipe.CreateNamedPipe(PIPE_NAME, win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT | win32pipe.PIPE_READMODE_MESSAGE, win32pipe.PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, 500, None) win32pipe.ConnectNamedPipe(named_pipe, None) while True: win32file.WriteFile('1'.encode())
def OpenPipe(self): #,pipeName = r'\\.\pipe\wireshark'): #self.sPipeName = pipeName if (self.os == 'nt'): self.p = win32pipe.CreateNamedPipe( self.sPipeName, win32pipe.PIPE_ACCESS_OUTBOUND, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 300, None) win32pipe.ConnectNamedPipe(self.p, None) elif (self.os == 'posix'): os.mkfifo(self.sPipeName) self.f = 0 #Remember to unlink the FIFO self.p = os.open(self.sPipeName, os.O_WRONLY)
def run(self, security_attributes): """ Handles the creation of the pipe. Windows SACL and DACL data for creating the pipe. :type security_attributes: win32security.SECURITY_ATTRIBUTES instance :return: None :rtype: None """ import eg eg.PrintDebugNotice('Named Pipe: Creating pipe {0}'.format( self._pipe_id)) pipe = win32pipe.CreateNamedPipe( r'\\.\pipe\eventghost', PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_WAIT | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, 4096, 4096, 5, security_attributes) win32pipe.ConnectNamedPipe(pipe, None) data = win32file.ReadFile(pipe, 4096) self.is_waiting = False if not self._parent.running_pipes[-1].is_waiting == self: self._parent.running_pipes += [ Pipe(self._parent, self._parent.get_pipe_id(), security_attributes) ] eg.PrintDebugNotice('Pipe {0}: Data received'.format(self._pipe_id)) if data[0] == 0: event = threading.Event() res = [''] self._parent.process_command.add(self._pipe_id, data[1], res, event) while not event.isSet(): pass win32file.WriteFile(pipe, str(repr(res[0]))) win32pipe.DisconnectNamedPipe(pipe) win32file.CloseHandle(pipe) else: try: raise NamedPipeDataError('Pipe {0}: Unknown Error: {1}'.format( self._pipe_id, str(data))) except NamedPipeDataError: traceback.print_exc() self._parent.running_pipes.remove(self)
def _create_fifo(self): try: self.named_pipe = win32pipe.CreateNamedPipe( self.out_fifo, win32pipe.PIPE_ACCESS_OUTBOUND, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 300, None) logger.info('Opened FIFO %s' % (self.out_fifo, )) except win32api.error, (code, fn, details): if code == 231: logger.warn('FIFO %s exists. Using it' % (self.out_fifo, )) else: raise
def open(self): import win32pipe self._pipe = win32pipe.CreateNamedPipe( self._pipeName, win32pipe.PIPE_ACCESS_OUTBOUND, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 300, None) if not self._pipe: raise SystemExit(1) win32pipe.ConnectNamedPipe(self._pipe, None)
def foo(self, n): while (1): p = win32pipe.CreateNamedPipe( r'\\.\pipe\gpvdm_pipe', win32pipe.PIPE_ACCESS_INBOUND, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT, 255, 65536, 65536, 300, None) win32pipe.ConnectNamedPipe(p, None) th = Thread(target=self.rod, args=(p, )) th.daemon = True th.start()
def writer(): import win32pipe, win32file p = win32pipe.CreateNamedPipe( r'\\.\pipe\test_pipe', win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 300, None) win32pipe.ConnectNamedPipe(p, None) data = b"Hello Pipe" win32file.WriteFile(p, data) win32file.CloseHandle(p)
def startPipeServer(self, event, wait_time=0): openMode = win32pipe.PIPE_ACCESS_DUPLEX pipeMode = win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT sa = pywintypes.SECURITY_ATTRIBUTES() sa.SetSecurityDescriptorDacl(1, None, 0) pipe_handle = win32pipe.CreateNamedPipe( self.pipename, openMode, pipeMode, win32pipe.PIPE_UNLIMITED_INSTANCES, 0, 0, 2000, sa) threading.Thread(target=self._serverThread, args=(pipe_handle, event, wait_time)).start()
def create_winpipe(): pipe = win32pipe.CreateNamedPipe( r'\\.\pipe\discord-ipc-0', win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_READMODE_BYTE | win32pipe.PIPE_WAIT, 1, 2048, 2048, 0, None) return pipe
def setup_pipe_win(self): INFO(1, None, None, "Opening PIPE %s" % self.ipc_obj_name) self.overlap_read.hEvent = win32event.CreateEvent(None, 0, 0, None) if self.overlap_read.hEvent == 0: ERROR("Failed to initialize named pipe") try: self.pipe_in = win32pipe.CreateNamedPipe( self.ipc_obj_name, win32pipe.PIPE_ACCESS_DUPLEX | win32file.FILE_FLAG_OVERLAPPED, 0, 1, 512, 512, 0, None) except OSError as e: ERROR("Failed to create named pipe %s! Error %s" % (self.ipc_obj_name, e.message))
def __init__(self, name: str, wait_connected: bool = False, block_io: bool = True): pipe = win32pipe.CreateNamedPipe( '\\\\.\\pipe\\' + name, win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | (win32pipe.PIPE_WAIT if block_io else win32pipe.PIPE_NOWAIT), 1, 65536, 65536, 0, None) root.info('WINAPI: Created named pipe %s: %s' % (name, str(pipe))) if wait_connected: win32pipe.ConnectNamedPipe(pipe, None) super(NamedPipeServer, self).__init__(pipe)
def _openPipe(self): """Internal function to open a named pipe.""" try: self.handle = win32pipe.CreateNamedPipe( "\\\\.\\pipe\\%s" % (self.name), self.openMode | win32file.FILE_FLAG_OVERLAPPED, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE, self.max, self.outsize, self.insize, 0, None) except pywintypes.error as e: if e.winerror == winerror.ERROR_PIPE_BUSY: raise PipeAlreadyExistsError(e.strerror) else: raise e
def __init__(self, pipename): Thread.__init__(self, name='PipeServer') self.daemon = True import win32pipe, win32api, win32con FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000 PIPE_REJECT_REMOTE_CLIENTS = 0x00000008 self.pipe_handle = win32pipe.CreateNamedPipe( pipename, win32pipe.PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE, win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_READMODE_BYTE | win32pipe.PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS, 1, 8192, 8192, 0, None) win32api.SetHandleInformation(self.pipe_handle, win32con.HANDLE_FLAG_INHERIT, 0) self.err_msg = None self.data = b'' self.start()
def CreateServer(): p = win32pipe.CreateNamedPipe( '\\\\.\\pipe\\test_pipe', win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 300, None) bufObject = win32file.AllocateReadBuffer(BUFFER_SIZE) data = 'Hello Pipe' for i in range(len(data)): bufObject[i] = ord(data[i]) win32pipe.ConnectNamedPipe(p, None) win32file.WriteFile(p, bufObject[0:len(data)])
def create_pipe(self): self.pipe = win32pipe.CreateNamedPipe(r'\\.\pipe\{name}'.format( name=self.name), win32pipe.PIPE_ACCESS_INBOUND, (win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT), 1, 1024, 1024, 0, None) if self.pipe == win32file.INVALID_HANDLE_VALUE: last_error = win32api.GetLastError() raise OSError(last_error)