def testCompletionPortsMultiple(self): # Mainly checking that we can "associate" an existing handle. This # failed in build 203. ioport = win32file.CreateIoCompletionPort( win32file.INVALID_HANDLE_VALUE, 0, 0, 0) socks = [] for PORT in range(9123, 9125): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('', PORT)) sock.listen(1) socks.append(sock) new = win32file.CreateIoCompletionPort(sock.fileno(), ioport, PORT, 0) assert new is ioport for s in socks: s.close() hv = int(ioport) ioport = new = None # The handle itself should be closed now (unless we leak references!) # Check that. try: win32file.CloseHandle(hv) raise RuntimeError("Expected close to fail!") except win32file.error as details: self.failUnlessEqual(details.winerror, winerror.ERROR_INVALID_HANDLE)
def testCompletionPortsNonQueued(self, test_overlapped_death = 0): # In 204 we had a reference count bug when OVERLAPPED objects were # associated with a completion port other than via # PostQueuedCompletionStatus. This test is based on the reproduction # reported with that bug. # Create the pipe. BUFSIZE = 512 pipe_name = r"\\.\pipe\pywin32_test_pipe" handle = win32pipe.CreateNamedPipe(pipe_name, 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) # Create an IOCP and associate it with the handle. port = win32file.CreateIoCompletionPort(-1, 0, 0, 0) win32file.CreateIoCompletionPort(handle, port, 1, 0) thread = threading.Thread(target=self._IOCPServerThread, args=(handle,port, test_overlapped_death)) thread.start() try: time.sleep(0.1) # let thread do its thing. try: win32pipe.CallNamedPipe(r"\\.\pipe\pywin32_test_pipe", "Hello there", BUFSIZE, 0) except win32pipe.error: # Testing for overlapped death causes this if not test_overlapped_death: raise finally: handle.Close() thread.join()
def __init__(self, onData = None, onResult = None, onAccept = None ): # def onData( fd, data, userdata , sockdata): # def onResult( fd, result, userdata, sockdata, byte ): # def onAccept( fd, insock, userdata, sockdata ): self.hiocp = win32file.CreateIoCompletionPort(-1,0,1,1) ol = pywintypes.OVERLAPPED() ol.Internal = 0 ol.InternalHigh = 0 ol.Offset = 0 ol.OffsetHigh = 0 ol.hEvent = 0 self.ol = ol self.timerol = pywintypes.OVERLAPPED() self.timerol.object = [1,1,1] self.allfun = [] self.onresult = onResult self.ondata = onData self.onaccept = onAccept self.fds = {} self.infds = {} self.stop = 0 import threading
def register(self, handle): """registers the given handle with the IOCP. the handle cannot be unregistered later. handle must be a windows handle, not a fileno""" if hasattr(handle, "handle"): handle = handle.handle if handle in self._handles: return win32file.CreateIoCompletionPort(handle, self._port, 0, 0) self._handles.add(handle)
def testCompletionPortsQueued(self): class Foo: pass io_req_port = win32file.CreateIoCompletionPort(-1, None, 0, 0) overlapped = pywintypes.OVERLAPPED() overlapped.object = Foo() win32file.PostQueuedCompletionStatus(io_req_port, 0, 99, overlapped) errCode, bytes, key, overlapped = \ win32file.GetQueuedCompletionStatus(io_req_port, win32event.INFINITE) self.failUnlessEqual(errCode, 0) self.failUnless(isinstance(overlapped.object, Foo))
def add_dir_watch(self, path, flags, recursive=False): try: flags |= FSEvent.DeleteSelf watch = FSMonitorWatch(path, flags, recursive) with self.__lock: key = self.__last_key win32file.CreateIoCompletionPort(watch._hDir, self.__cphandle, key, 0) self.__last_key += 1 read_changes(watch) watch._key = key self.__key_to_watch[key] = watch return watch except pywintypes.error, e: raise FSMonitorWindowsError(*e.args)
def addsocket( self, sock, data = None ): fd = sock.fileno() win32file.CreateIoCompletionPort( fd, self.hiocp, 0,1) sendol = pywintypes.OVERLAPPED() sendol.Internal = 0 sendol.InternalHigh = 0 sendol.Offset = 0 sendol.OffsetHigh = 0 sendol.hEvent = 0 recvol = pywintypes.OVERLAPPED() recvol.Internal = 0 recvol.InternalHigh = 0 recvol.Offset = 0 recvol.OffsetHigh = 0 recvol.hEvent = 0 self.fds[sock] = [sendol, recvol , win32file.AllocateReadBuffer(1024000), data]
addreses = socket.getaddrinfo(server_ip, server_port, socket.AF_INET, 0, socket.SOL_UDP) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) for addr in addreses: try: sock.connect(addr[4]) break except socket.error as e: print 'connect error: ', e print 'connect OK' signal.signal(signal.SIGINT, sig_handler) completion_port = win32file.CreateIoCompletionPort( win32file.INVALID_HANDLE_VALUE, None, 0, 0) print win32file.CreateIoCompletionPort(handle, completion_port, 111, 0) print win32file.CreateIoCompletionPort(sock.fileno(), completion_port, 222, 0) tun_recv = TunnelRecv() net_recv = NetworkRecv() timer = TimerThread(1) # per second timer.start() while running: timeout = last_send + keepalive_interval - now rc, numberOfBytesTransferred, completionKey, overlapped = win32file.GetQueuedCompletionStatus( completion_port, int(1000 * timeout)) if rc:
def __init__(self): self._port = win32file.CreateIoCompletionPort( win32file.INVALID_HANDLE_VALUE, None, 0, 0) self._key = itertools.count()
def add_handle(self, handle): if hasattr(handle, "fileno"): handle = handle.fileno() key = self._key.next() win32file.CreateIoCompletionPort(handle, self._port, key, 0) return key
def __init__(self): self._port = win32file.CreateIoCompletionPort(win32file.INVALID_HANDLE_VALUE, None, 0, 0) self._post_overlapped = win32file.OVERLAPPED() self._handles = set()
def pipe_server(): pipe = win32pipe.CreateNamedPipe( r'\\.\pipe\my_pipe', win32pipe.PIPE_ACCESS_INBOUND | win32file.FILE_FLAG_OVERLAPPED, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 0, None) quit = False overlapped_obj1 = pywintypes.OVERLAPPED() completion_port = win32file.CreateIoCompletionPort(pipe, None, 0, 0) while not quit: try: win32pipe.ConnectNamedPipe(pipe, overlapped_obj1) while True: # Doing some sleep (2 seconds): Actually, other things could be done in this time for i in range(10): time.sleep(0.2) # If the function succeeds, rc will be set to 0, otherwise it will be set to the win32 error code (258). (rc, byte_num, key, ovrlpd) = win32file.GetQueuedCompletionStatus( completion_port, 50) #1000) if rc == 0: # The message can be retrieved msg = '' # Will contain the whole message at the end """ in order to read the characters we need to distinguish two cases: -the character is a normal (unicode) character, which takes 1 byte -the character is non_unicode (German, Russian...), which takes 2 bytes as we are reading 1 byte at a time, the above-mentioned discussion is essential. When a 1 byte character is coming, its decoding is done without problems. Let's see what happens, when a 2-byte character is coming. In this case, we read 1 byte, try to decode, get an error. Now we know that the character takes 2-bytes. Then, we read 1 byte again, combine them and decode. """ # 1-byte character case try: rtnvalue, data = win32file.ReadFile( pipe, 1, overlapped_obj1) msg = msg + bytes(data).decode("utf-8") # 2-bytes character case except: rtnvalue, data1 = win32file.ReadFile( pipe, 1, overlapped_obj1) non_unicode = data.tobytes() + data1.tobytes() msg = msg + non_unicode.decode("utf-8") while rtnvalue == 234: # more data is available try: # 1-byte character case rtnvalue, data = win32file.ReadFile( pipe, 1, overlapped_obj1) msg = msg + bytes(data).decode("utf-8") except: # 2-bytes character case rtnvalue, data1 = win32file.ReadFile( pipe, 1, overlapped_obj1) non_unicode = data.tobytes() + data1.tobytes() msg = msg + non_unicode.decode("utf-8") # the next line helps with writing Russian and Armenian characters into a text file with codecs.open('test.txt', 'w', encoding='utf-8') as f: f.write(msg) f.close() print("Successfully received and written") print("Closing Handle") exit(0) except pywintypes.error as e: if e.args[0] == 109: print("broken pipe") quit = True else: print(e) quit = True win32file.CloseHandle(pipe)
def __init__(self): self._port = win32file.CreateIoCompletionPort(win32file.INVALID_FILE_HANDLE, None, 0, 0)
def add_handle(self, handle): win32file.CreateIoCompletionPort(handle, self._port, int(port), 0)
def listen(self, port, data = None ): sock = self.ListenSocket( port, data ) win32file.CreateIoCompletionPort( sock.socket().fileno(), self.hiocp, 0,1) self.infds[sock.socket()] = sock sock.insock = socket.socket( socket.AF_INET, socket.SOCK_STREAM) win32file.AcceptEx( sock.socket(), sock.insock, sock.buffer, sock.ol )
def __init__(self): self.__key_to_watch = {} self.__last_key = 0 self.__lock = threading.Lock() self.__cphandle = win32file.CreateIoCompletionPort(-1, None, 0, 0)
def main(): # escape list of arguments command = _win32_arglist_to_string(sys.argv[1:]) # create job hJob = win32job.CreateJobObject(None, '') extended_info = win32job.QueryInformationJobObject( hJob, win32job.JobObjectExtendedLimitInformation) extended_info['BasicLimitInformation'][ 'LimitFlags'] = win32job.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE win32job.SetInformationJobObject( hJob, win32job.JobObjectExtendedLimitInformation, extended_info) # associate job with completion port hIoPort = win32file.CreateIoCompletionPort(win32file.INVALID_HANDLE_VALUE, None, 0, 1) # pywin32 is missing support for JOBOBJECT_ASSOCIATE_COMPLETION_PORT, therefore # we call it through ctypes port = JOBOBJECT_ASSOCIATE_COMPLETION_PORT() port.CompletionKey = hJob.handle port.CompletionPort = hIoPort.handle assert bool( ctypes.windll.kernel32.SetInformationJobObject( ctypes.wintypes.HANDLE(hJob.handle), ctypes.c_int(JobObjectAssociateCompletionPortInformation), ctypes.byref(port), ctypes.sizeof(JOBOBJECT_ASSOCIATE_COMPLETION_PORT))) # create process suspended si = win32process.STARTUPINFO() hProcess, hThread, processId, threadId = win32process.CreateProcess( None, command, None, None, True, win32process.CREATE_BREAKAWAY_FROM_JOB | win32process.CREATE_SUSPENDED, None, None, si) # add process to job win32job.AssignProcessToJobObject(hJob, hProcess) # resume process win32process.ResumeThread(hThread) win32api.CloseHandle(hThread) win32api.CloseHandle(hProcess) # wait for job termination numberOfBytes = ctypes.wintypes.DWORD(0) completionKey = ctypes.wintypes.HANDLE(0) overlapped = OVERLAPPED() while True: # calling this through pywin32 crashes the program, therefore we call it through ctypes res = bool( ctypes.windll.kernel32.GetQueuedCompletionStatus( ctypes.wintypes.HANDLE(hIoPort.handle), ctypes.byref(numberOfBytes), ctypes.byref(completionKey), ctypes.byref(overlapped), ctypes.wintypes.DWORD(win32event.INFINITE))) if not res or (bytes(completionKey) == bytes( ctypes.c_void_p(hJob.handle)) and bytes(numberOfBytes) == bytes( ctypes.c_ulong( win32job.JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO))): break
def __init__(self, object): self.port = win32file.CreateIoCompletionPort(-1, 0, 0, 0) win32file.CreateIoCompletionPort(object.handle, self.port, 1, 0)