如何判断附加到我的stdios的文件(或tty)?
就像是:
>>> import sys
>>> print sys.stdin.__path__
'/dev/tty1'
>>>
Run Code Online (Sandbox Code Playgroud)
我可以看看proc:
import os, sys
os.readlink('/proc/self/fd/%s' % sys.stdin.fileno())
Run Code Online (Sandbox Code Playgroud)
但似乎应该有内置的方式?
用os.pipe()
它创建管道时返回2个文件号; 读取端和写入端,可以用os.write()
/ 写入和读取os.read()
; 没有os.readline().是否可以使用readline?
import os
readEnd, writeEnd = os.pipe()
# something somewhere writes to the pipe
firstLine = readEnd.readline() #doesn't work; os.pipe returns just fd numbers
Run Code Online (Sandbox Code Playgroud)
简而言之,当你拥有的是文件句柄号时,是否可以使用readline?
我试图reboot
通过Python中的libc 调用函数ctypes
,我只是无法让它工作.我一直在引用该man 2 reboot
页面(http://linux.die.net/man/2/reboot).我的内核版本是2.6.35.
下面是来自交互式Python提示的控制台日志,我试图让我的机器重新启动 - 我做错了什么?
为什么不ctypes.get_errno()
工作?
>>> from ctypes import CDLL, get_errno
>>> libc = CDLL('libc.so.6')
>>> libc.reboot(0xfee1dead, 537993216, 0x1234567, 0)
-1
>>> get_errno()
0
>>> libc.reboot(0xfee1dead, 537993216, 0x1234567)
-1
>>> get_errno()
0
>>> from ctypes import c_uint32
>>> libc.reboot(c_uint32(0xfee1dead), c_uint32(672274793), c_uint32(0x1234567), c_uint32(0))
-1
>>> get_errno()
0
>>> libc.reboot(c_uint32(0xfee1dead), c_uint32(672274793), c_uint32(0x1234567))
-1
>>> get_errno()
0
>>>
Run Code Online (Sandbox Code Playgroud)
编辑:
通过Nemos提醒 - 我可以get_errno
返回22(无效参数).不足为奇.我该怎么打电话reboot()
?我显然没有传递函数期望的参数.=)
在模块代码中初始化模块中的对象是不好的做法吗?
在Module.py
:
class _Foo(object):
def __init__(self):
self.x = 'Foo'
Foo = _Foo()
Run Code Online (Sandbox Code Playgroud)
比在用户代码中,您可以:
>>> from Module import Foo
>>> print Foo.x
'Foo'
>>>
Run Code Online (Sandbox Code Playgroud)
...无需在用户代码中初始化 Foo 类。当然,只有在不需要参数来初始化对象时才有用。
有理由不这样做吗?
这不起作用:
class ifinfomsg(ctypes.Structure):
_fields_ = [
('ifi_family', ctypes.c_ubyte),
('__ifi_pad', ctypes.c_ubyte),
('ifi_type', ctypes.c_ushort),
('ifi_index', ctypes.c_int),
('ifi_flags', ctypes.c_uint),
('ifi_change', ctypes.c_uint(0xFFFFFFFF))
]
Run Code Online (Sandbox Code Playgroud)
它的错误是:
File "rtnetlink.py", line 243, in <module>
class ifinfomsg(ctypes.Structure):
TypeError: Error when calling the metaclass bases
second item in _fields_ tuple (index 5) must be a C type
Run Code Online (Sandbox Code Playgroud)
但是我可以设置以下值__init__()
:
class ifinfomsg(ctypes.Structure):
_fields_ = [
('ifi_family', ctypes.c_ubyte),
('__ifi_pad', ctypes.c_ubyte),
('ifi_type', ctypes.c_ushort),
('ifi_index', ctypes.c_int),
('ifi_flags', ctypes.c_uint),
('ifi_change', ctypes.c_uint)
]
def __init__(self):
self.__ifi_pad = 0
self.ifi_change = 0xFFFFFFFF
Run Code Online (Sandbox Code Playgroud)
这是通过 执行此操作的“正确”方法吗__init__
?
我发布了Python找到关于尝试找到第一跳的第一个网络跳,我想的越多,它就越容易成为python中的路由表的过程.我不是程序员,我不知道我在做什么.:p
这就是我提出的,我注意到的第一个问题是环回接口没有显示在/ proc/net/route文件中 - 所以评估127.0.0.0/8会给你默认路由...对于我申请,没关系.
还有什么重要我忽略了吗?解析ip route get <ip>
仍然是一个更好的主意吗?
import re
import struct
import socket
'''
Read all the routes into a list. Most specific first.
# eth0 000219AC 04001EAC 0003 0 0 0 00FFFFFF ...
'''
def _RtTable():
_rt = []
rt_m = re.compile('^[a-z0-9]*\W([0-9A-F]{8})\W([0-9A-F]{8})[\W0-9]*([0-9A-F]{8})')
rt = open('/proc/net/route', 'r')
for line in rt.read().split('\n'):
if rt_m.match(line):
_rt.append(rt_m.findall(line)[0])
rt.close()
return _rt
'''
Create a temp ip (tip) that is the entered ip with the host
section …
Run Code Online (Sandbox Code Playgroud) 这是一个非常简单的任务,我觉得我应该能够做到 - 但仅仅是为了我的生活,无法弄清楚.
我正在尝试编写一个递归函数来复制以下内容:
chars = '0123456789abcdef'
for a in chars:
for b in chars:
for c in chars:
for d in chars:
print a+b+c+d
Run Code Online (Sandbox Code Playgroud)
寻找一个例子并没有证明是非常有成效的.
代码不起作用:
chars = 'ABCDEF'
def resu(chars, depth = len(chars)):
for char in chars:
if depth == 0:
return char
return char + resu(chars, depth - 1)
print resu(chars)
Run Code Online (Sandbox Code Playgroud) 做这个:
import foo as bar
Run Code Online (Sandbox Code Playgroud)
做同样的事情吗?
bar = __import__('foo')
Run Code Online (Sandbox Code Playgroud)
是否有理由使用后者?
我正在阅读别人的代码,我找到了后者,并且不确定他们为什么不使用先前的语法
是否有一个简单的工具,或者可能是一种将 strace 输出转换为可以可视化或更容易筛选的东西的方法?我必须找出应用程序出错的地方,但跟踪它会产生大量数据。试图在更大范围内跟踪此应用程序及其线程正在执行(或尝试执行)的操作被证明很难读取每个系统调用。
我没有任何预算,我们是一家纯粹的 Linux 商店。
我有一个Structure
(在这种情况下是一个Netlink消息头),我需要通过套接字发送到内核.我弄清楚的唯一方法是使用__reduce__()
.
>>> class nlmsghdr(ctypes.Structure):
... _fields_ = [('nlmsg_len', ctypes.c_int32),
... ('nlmsg_type', ctypes.c_int16),
... ('nlmsg_flags', ctypes.c_int16),
... ('nlmsg_seq', ctypes.c_int32),
... ('nlmsg_pid', ctypes.c_int32)]
...
>>>
>>> hdr = nlmsghdr(20, 22, 769, 1328884876, 0)
>>> hdr.__reduce__()[1][1][1]
'\x14\x00\x00\x00\x16\x00\x01\x03\x8c,5O\x00\x00\x00\x00'
>>> # socket.send(hdr.__reduce__()[1][1][1])
Run Code Online (Sandbox Code Playgroud)
它看起来像是__reduce__
序列化(pickle)并依赖于它总是以相同的方式运行似乎是一个错误.
肯定有更好的办法?