本文整理汇总了Python中fcntl.fcntl方法的典型用法代码示例。如果您正苦于以下问题:Python fcntl.fcntl方法的具体用法?Python fcntl.fcntl怎么用?Python fcntl.fcntl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fcntl
的用法示例。
在下文中一共展示了fcntl.fcntl方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _set_cloexec
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def _set_cloexec(self):
"""Set the CLOEXEC flag on all open files (except stdin/out/err).
If self.max_cloexec_files is an integer (the default), then on
platforms which support it, it represents the max open files setting
for the operating system. This function will be called just before
the process is restarted via os.execv() to prevent open files
from persisting into the new process.
Set self.max_cloexec_files to 0 to disable this behavior.
"""
for fd in range(3, self.max_cloexec_files): # skip stdin/out/err
try:
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
except IOError:
continue
fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
示例2: mac_set_ip_address
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def mac_set_ip_address(self, dev, ip, serverip, netmask):
ifr = struct.pack('<16sBBHIIIBBHIIIBBHIII',
self.iface_name,
16, socket.AF_INET, 0, struct.unpack('<L', socket.inet_pton(socket.AF_INET, ip))[0], 0, 0,
16, socket.AF_INET, 0, struct.unpack('<L', socket.inet_pton(socket.AF_INET, serverip))[0], 0, 0,
16, 0, 0, struct.unpack('<L', socket.inet_pton(socket.AF_INET, "255.255.255.255"))[0], 0, 0)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
fcntl.ioctl(sock, self.IOCTL_MACOSX_SIOCAIFADDR, ifr)
except Exception as e:
common.internal_print("Something went wrong with setting up the interface.", -1)
print(e)
sys.exit(-1)
# adding new route for forwarding packets properly.
integer_ip = struct.unpack(">I", socket.inet_pton(socket.AF_INET, serverip))[0]
rangeip = socket.inet_ntop(socket.AF_INET, struct.pack(">I", integer_ip & ((2**int(netmask))-1)<<32-int(netmask)))
ps = subprocess.Popen(["route", "add", "-net", rangeip+"/"+netmask, serverip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = ps.communicate()
if stderr:
if not "File exists" in stderr:
common.internal_print("Error: adding client route: {0}".format(stderr), -1)
sys.exit(-1)
return
示例3: freebsd_tun_alloc
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def freebsd_tun_alloc(self, dev, flags):
try:
sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ifr = struct.pack('<16si', 'tun', 0)
self.iface_name = fcntl.ioctl(sockfd, self.IOCTL_FREEBSD_SIOCIFCREATE2, ifr)
self.iface_name = self.iface_name.rstrip("\x00")
buff = array.array('c', dev+"\x00")
caddr_t, _ = buff.buffer_info()
ifr = struct.pack('16sP', self.iface_name, caddr_t);
fcntl.ioctl(sockfd, self.IOCTL_FREEBSD_SIOCSIFNAME, ifr)
tun = os.open("/dev/"+self.iface_name, os.O_RDWR | os.O_NONBLOCK)
self.iface_name = dev
except IOError as e:
print e
common.internal_print("Error: Cannot create tunnel. Is {0} in use?".format(dev), -1)
sys.exit(-1)
return tun
示例4: read_char_no_blocking
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def read_char_no_blocking():
''' Read a character in nonblocking mode, if no characters are present in the buffer, return an empty string '''
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
old_flags = fcntl.fcntl(fd, fcntl.F_GETFL)
try:
tty.setraw(fd, termios.TCSADRAIN)
fcntl.fcntl(fd, fcntl.F_SETFL, old_flags | os.O_NONBLOCK)
return sys.stdin.read(1)
except IOError as e:
ErrorNumber = e[0]
# IOError with ErrorNumber 11(35 in Mac) is thrown when there is nothing to read(Resource temporarily unavailable)
if (sys.platform.startswith("linux") and ErrorNumber != 11) or (sys.platform == "darwin" and ErrorNumber != 35):
raise
return ""
finally:
fcntl.fcntl(fd, fcntl.F_SETFL, old_flags)
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
示例5: train
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def train(self):
i=0
if(self.args.ipython):
import fcntl
fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
while((i < self.total_steps or self.total_steps == -1) and not self.gan.destroy):
i+=1
start_time = time.time()
self.step()
GlobalViewer.tick()
if (self.args.save_every != None and
self.args.save_every != -1 and
self.args.save_every > 0 and
i % self.args.save_every == 0):
print(" |= Saving network")
self.gan.save(self.save_file)
if self.args.ipython:
self.check_stdin()
end_time = time.time()
示例6: blocking
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def blocking(self, port, mode=False):
"""
Set port function mode blocking/nonblocking
:param port: port to set mode
:param mode: False to set nonblock mode, True for block mode
"""
fd = self._open([port])[0]
try:
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
if not mode:
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
else:
fcntl.fcntl(fd, fcntl.F_SETFL, fl & ~os.O_NONBLOCK)
except Exception as inst:
print("FAIL: Setting (non)blocking mode: " + str(inst))
return
if mode:
print("PASS: set to blocking mode")
else:
print("PASS: set to nonblocking mode")
示例7: set_fd_flag
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def set_fd_flag(fd, flag):
"""Set a flag on a file descriptor.
``fd`` is the file descriptor or file object, ``flag`` the flag as integer.
"""
flags = fcntl.fcntl(fd, fcntl.F_GETFD, 0)
fcntl.fcntl(fd, fcntl.F_SETFD, flags | flag)
示例8: set_fd_status_flag
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def set_fd_status_flag(fd, flag):
"""Set a status flag on a file descriptor.
``fd`` is the file descriptor or file object, ``flag`` the flag as integer.
"""
flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
fcntl.fcntl(fd, fcntl.F_SETFL, flags | flag)
示例9: lin_init
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def lin_init(self):
global fcntl
import fcntl
示例10: lin_tun_alloc
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def lin_tun_alloc(self, dev, flags):
try:
tun = os.open(Interface.LINUX_CLONEDEV, os.O_RDWR|os.O_NONBLOCK, 0)
ifr = struct.pack('16sH', dev, flags)
fcntl.ioctl(tun, self.IOCTL_LINUX_TUNSETIFF, ifr)
except IOError:
common.internal_print("Error: Cannot create tunnel. Is {0} in use?".format(dev), -1)
sys.exit(-1)
return tun
示例11: lin_set_ip_address
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def lin_set_ip_address(self, dev, ip, serverip, netmask):
sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# set IP
ifr = struct.pack('<16sH2s4s8s', dev, socket.AF_INET, "\x00"*2, socket.inet_aton(ip), "\x00"*8)
fcntl.ioctl(sockfd, self.IOCTL_LINUX_SIOCSIFADDR, ifr)
# get flags
ifr = struct.pack('<16sh', dev, 0)
flags = struct.unpack('<16sh', fcntl.ioctl(sockfd, self.IOCTL_LINUX_SIOCSIFFLAGS, ifr))[1]
# set new flags
flags = flags | self.IOCTL_LINUX_IFF_UP
ifr = struct.pack('<16sh', dev, flags)
# iface up
fcntl.ioctl(sockfd, self.IOCTL_LINUX_SIOCSIFFLAGS, ifr)
except Exception as e:
common.internal_print("Something went wrong with setting up the interface.", -1)
print(e)
sys.exit(-1)
# adding new route for forwarding packets properly.
integer_ip = struct.unpack(">I", socket.inet_pton(socket.AF_INET, serverip))[0]
rangeip = socket.inet_ntop(socket.AF_INET, struct.pack(">I", integer_ip & ((2**int(netmask))-1)<<32-int(netmask)))
integer_netmask = struct.pack(">I", ((2**int(netmask))-1)<<32-int(netmask))
netmask = socket.inet_ntoa(integer_netmask)
ps = subprocess.Popen(["route", "add", "-net", rangeip, "netmask", netmask, "dev", dev], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = ps.communicate()
if stderr:
if not "File exists" in stderr:
common.internal_print("Error: adding client route: {0}".format(stderr), -1)
sys.exit(-1)
return
示例12: lin_set_mtu
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def lin_set_mtu(self, dev, mtu):
s = socket.socket(type=socket.SOCK_DGRAM)
try:
ifr = struct.pack('<16sH', dev, mtu) + '\x00'*14
fcntl.ioctl(s, self.IOCTL_LINUX_SIOCSIFMTU, ifr)
except Exception as e:
common.internal_print("Cannot set MTU ({0}) on interface".format(mtu), -1)
sys.exit(-1)
return
示例13: mac_init
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def mac_init(self):
global fcntl
import fcntl
示例14: mac_set_mtu
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def mac_set_mtu(self, dev, mtu):
s = socket.socket(type=socket.SOCK_DGRAM)
try:
ifr = struct.pack('<16sH', self.iface_name, 1350)+'\x00'*14
fcntl.ioctl(s, self.IOCTL_MACOSX_SIOCSIFMTU, ifr)
except Exception as e:
common.internal_print("Cannot set MTU ({0}) on interface".format(mtu), -1)
sys.exit(-1)
return
示例15: freebsd_init
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import fcntl [as 别名]
def freebsd_init(self):
global fcntl
import fcntl
# @sghctoma for the win. Thanks for the help to support FreeBSD.