本文整理汇总了Python中win32file.WriteFile方法的典型用法代码示例。如果您正苦于以下问题:Python win32file.WriteFile方法的具体用法?Python win32file.WriteFile怎么用?Python win32file.WriteFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类win32file
的用法示例。
在下文中一共展示了win32file.WriteFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ZapMBRGPT
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def ZapMBRGPT(self, disk_size, sector_size, add1MB):
self.assert_physical_drive()
# Implementation borrowed from rufus: https://github.com/pbatard/rufus
num_sectors_to_clear \
= (add1MB and 2048 or 0) + self.MAX_SECTORS_TO_CLEAR
zeroBuf = b'\0' * sector_size
for i in range(num_sectors_to_clear):
self.WriteFile(zeroBuf)
offset = disk_size - self.MAX_SECTORS_TO_CLEAR * sector_size
win32file.SetFilePointer(self.h, offset, win32con.FILE_BEGIN)
for i in range(num_sectors_to_clear):
self.WriteFile(zeroBuf)
# We need to append paddings as CREATE_DISK structure contains a union.
param = struct.pack('<IIIHH8s',
winioctlcon.PARTITION_STYLE_MBR, 0xdeadbeef,
0,0,0,b'abcdefgh')
win32file.DeviceIoControl(
self.h, winioctlcon.IOCTL_DISK_CREATE_DISK, param, 0, None)
示例2: SimpleFileDemo
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def SimpleFileDemo():
testName = os.path.join( win32api.GetTempPath(), "win32file_demo_test_file")
if os.path.exists(testName): os.unlink(testName)
# Open the file for writing.
handle = win32file.CreateFile(testName,
win32file.GENERIC_WRITE,
0,
None,
win32con.CREATE_NEW,
0,
None)
test_data = "Hello\0there".encode("ascii")
win32file.WriteFile(handle, test_data)
handle.Close()
# Open it for reading.
handle = win32file.CreateFile(testName, win32file.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None)
rc, data = win32file.ReadFile(handle, 1024)
handle.Close()
if data == test_data:
print "Successfully wrote and read a file"
else:
raise Exception("Got different data back???")
os.unlink(testName)
示例3: testSimpleFiles
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def testSimpleFiles(self):
try:
fd, filename = tempfile.mkstemp()
except AttributeError:
self.fail("This test requires Python 2.3 or later")
os.close(fd)
os.unlink(filename)
handle = win32file.CreateFile(filename, win32file.GENERIC_WRITE, 0, None, win32con.CREATE_NEW, 0, None)
test_data = str2bytes("Hello\0there")
try:
win32file.WriteFile(handle, test_data)
handle.Close()
# Try and open for read
handle = win32file.CreateFile(filename, win32file.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None)
rc, data = win32file.ReadFile(handle, 1024)
self.assertEquals(data, test_data)
finally:
handle.Close()
try:
os.unlink(filename)
except os.error:
pass
# A simple test using normal read/write operations.
示例4: _IOCPServerThread
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def _IOCPServerThread(self, handle, port, drop_overlapped_reference):
overlapped = pywintypes.OVERLAPPED()
win32pipe.ConnectNamedPipe(handle, overlapped)
if drop_overlapped_reference:
# Be naughty - the overlapped object is now dead, but
# GetQueuedCompletionStatus will still find it. Our check of
# reference counting should catch that error.
overlapped = None
# even if we fail, be sure to close the handle; prevents hangs
# on Vista 64...
try:
self.failUnlessRaises(RuntimeError,
win32file.GetQueuedCompletionStatus, port, -1)
finally:
handle.Close()
return
result = win32file.GetQueuedCompletionStatus(port, -1)
ol2 = result[-1]
self.failUnless(ol2 is overlapped)
data = win32file.ReadFile(handle, 512)[1]
win32file.WriteFile(handle, data)
示例5: _none_mode
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def _none_mode(self):
"""
Read and write to device in blocking mode
"""
data = ""
while not self.exit_thread.isSet():
data = ""
for desc in self.in_files:
ret, _data = win32file.ReadFile(desc, self.cachesize)
if ret:
msg = ("Error occurred while receiving data, "
"err=%s, read=%s" % (ret, _data))
print("FAIL: " + msg)
raise IOError(msg)
data += _data
if data != "":
for desc in self.out_files:
ret, _data = win32file.WriteFile(desc, data)
if ret:
msg = ("Error occurred while sending data, "
"err=%s, sentlen=%s" % (ret, _data))
print("FAIL: " + msg)
raise IOError(msg)
示例6: send
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def send(self, input):
input = to_bytes(input)
if not self.stdin:
return None
try:
x = msvcrt.get_osfhandle(self.stdin.fileno())
(errCode, written) = WriteFile(x, input)
except ValueError:
return self._close('stdin')
except (subprocess.pywintypes.error, Exception) as why:
if why.args[0] in (109, errno.ESHUTDOWN):
return self._close('stdin')
raise
return written
示例7: Stop
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def Stop(self):
"""
This will be called to stop the thread.
"""
if self.file:
writeOvlap = win32file.OVERLAPPED()
writeOvlap.hEvent = win32event.CreateEvent(None, 0, 0, None)
msg = "q".encode("ascii")
win32file.WriteFile(self.file, msg, writeOvlap)
win32file.CloseHandle(self.file)
self.file = None
self.keepRunning = False
if self.service:
win32service.CloseServiceHandle(self.service)
#eg.PrintNotice("MCE_Vista: stopping thread")
示例8: Transmit
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def Transmit(self, transmitData):
"""
This will be called to detect available IR Blasters.
"""
if not self.file:
if not self.connecting:
self.Connect()
else:
return False
while self.receiving:
time.sleep(0.05)
writeOvlap = win32file.OVERLAPPED()
writeOvlap.hEvent = win32event.CreateEvent(None, 0, 0, None)
win32file.WriteFile(self.file, transmitData, writeOvlap)
win32event.WaitForSingleObject(writeOvlap.hEvent, win32event.INFINITE)
return True
示例9: WriteFile
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def WriteFile(self, b):
return win32file.WriteFile(self.h, b, None)
示例10: CopyFrom
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def CopyFrom(self, src_file, progress_cb):
with openHandle(src_file, True, False,
lambda s:sys.stdout.write(s+'\n')) as src:
total_bytes = 0
hr, b = src.ReadFile(1024*1024)
# win32file.ReadFile() seems to have a bug in the interpretation
# of 'hr'. https://sourceforge.net/p/pywin32/bugs/689/
# The following loop condition is a workaround, which may not
# work properly.
while hr == 0 and len(b):
win32file.WriteFile(self.h, b, None)
total_bytes += len(b)
progress_cb(total_bytes)
hr, b = src.ReadFile(1024*1024)
示例11: send
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def send(self, channel_type, message, additional_data):
if channel_type == common.CONTROL_CHANNEL_BYTE:
transformed_message = self.transform(self.encryption, common.CONTROL_CHANNEL_BYTE+message, 1)
else:
transformed_message = self.transform(self.encryption, common.DATA_CHANNEL_BYTE+message, 1)
common.internal_print("RDP sent: {0}".format(len(transformed_message)), 0, self.verbosity, common.DEBUG)
overlapped_write = pywintypes.OVERLAPPED()
try:
win32file.WriteFile(self.comms_socket, struct.pack(">H", len(transformed_message))+transformed_message, overlapped_write)
except Exception as e:
raise
示例12: packet_writer_win
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def packet_writer_win(self, packet):
import pywintypes
import win32file
overlapped_write = pywintypes.OVERLAPPED()
win32file.WriteFile(self.tunnel, packet, overlapped_write)
return
# on MacOS(X) utun, all packets needs to be prefixed with 4 specific bytes
示例13: packet_writer_win
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def packet_writer_win(self, packet):
import pywintypes
import win32file
overlapped_write = pywintypes.OVERLAPPED()
win32file.WriteFile(self.tunnel_w, packet, overlapped_write)
return
# on MacOS(X) utun, all packets needs to be prefixed with 4 specific bytes
示例14: write
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def write(self, offset, data):
win32file.SetFilePointer(self.fhandle, offset, 0)
# The WinPmem driver returns bytes_written == 0 always. This is probably
# a bug in its write routine, so we ignore it here. If the operation was
# successful we assume all bytes were written.
err, _bytes_written = win32file.WriteFile(self.fhandle, data)
if err == 0:
return len(data)
return 0
示例15: transmit
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import WriteFile [as 别名]
def transmit(self,dataToTransmit):
# convert to string
dataToTransmit = ''.join([chr(b) for b in dataToTransmit])
# write over tuntap interface
win32file.WriteFile(self.tuntap, dataToTransmit, self.overlappedTx)
win32event.WaitForSingleObject(self.overlappedTx.hEvent, win32event.INFINITE)
self.overlappedTx.Offset = self.overlappedTx.Offset + len(dataToTransmit)
#======================== private =========================================