小编tMC*_*tMC的帖子

Python distutils gcc路径

我正在尝试交叉编译pycrypto包,但是我越来越近,但是我遇到了一个我无法弄清楚的问题.

我希望distutils使用交叉编译特定的gcc-所以我设置CC env var并且它似乎尊重编译器的第一次调用的设置,但就是这样.

export CC="/opt/teeos/buildroot/output/host/usr/bin/i586-linux-gcc"
/opt/teeos/buildroot/output/host/usr/bin/i586-linux-gcc -fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes -fPIC -std=c99 --sysroot=/opt/teeos/buildroot/output/staging -I/opt/teeos/buildroot/output/staging/usr/include/python2.7 -O3 -fomit-frame-pointer -Isrc/ -I/usr/include/python2.7 -c src/_fastmath.c -o build/temp.linux-i686-2.7/src/_fastmath.o
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions build/temp.linux-i686-2.7/src/_fastmath.o -lgmp -o build/lib.linux-i686-2.7/Crypto/PublicKey/_fastmath.so
unable to execute gcc: No such file or directory
Run Code Online (Sandbox Code Playgroud)

我暂时移动了我的系统gcc,因此无法找到它.

如何使distutils尊重CC=/opt/buildroot...每次调用编译器的选项/设置我希望distutils使用的GCC/LD路径?

python distutils compilation

7
推荐指数
1
解决办法
5065
查看次数

是否可以对Python中的字符串执行按位运算?

这失败了,这并不奇怪:

>>> 'abc' << 8
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for <<: 'str' and 'int'
>>> 
Run Code Online (Sandbox Code Playgroud)

ascii abc等于0110000101100010011000116382179是否有一种方法可以将它移动一些任意数量,这样'abc' << 8会是01100001011000100110001100000000什么?

其他按位操作怎么样?'abc' & 63= 100011等?

python string bit-manipulation

7
推荐指数
2
解决办法
1万
查看次数

了解Python Pickle Insecurity

它在Python文档中声明pickle不安全,不应解析不受信任的用户输入.如果你研究这个; 几乎所有的例子都system()通过调用来证明这一点os.system.

对我来说不清楚的os.system是,如果没有os导入模块,如何正确解释.

>>> import pickle
>>> pickle.loads("cos\nsystem\n(S'ls /'\ntR.") # This clearly works.
bin  boot  cgroup  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  selinux  srv  sys  tmp  usr  var
0
>>> dir() # no os module
['__builtins__', '__doc__', '__name__', '__package__', 'pickle']
>>> os.system('ls /')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> 
Run Code Online (Sandbox Code Playgroud)

谁能解释一下?

python security namespaces pickle

7
推荐指数
3
解决办法
1340
查看次数

Python TTY控件

我想我不清楚getty/agetty/mgetty程序在linux/unix机器上的功能是什么.我可以用这样的东西在tty上启动一个shell:

TTY = '/dev/tty3'

cpid = os.fork()
if cpid == 0:
    os.closerange(0, 4)
    sys.stdin = open(TTY, 'r')
    sys.stdout = open(TTY, 'w')
    sys.stderr = open(TTY, 'w')
    os.execv(('/bin/bash',), ('bash',))
Run Code Online (Sandbox Code Playgroud)

..如果我切换到tty3,有一个shell运行 - 但一些键击被忽略/永远不会被发送到shell.shell知道TTY设置不正确,因为bash会说"无法打开tty,禁止作业控制"

我知道'termios'模块具有更改TTY设置的功能,这是'tty'模块使用的设置,但是我无法找到python正确设置TTY并启动shell的示例.我觉得它应该是简单的东西,但我不知道在哪里看.

查看*etty程序的来源并没有帮助我 - 对我来说C看起来像希腊语: - /

也许我只是没有找到合适的条款?有人在过去用Python取代了*etty程序并得到了他们想要分享的解释吗?

感谢您娱乐我的基本问题:)

python unix linux tty termios

6
推荐指数
1
解决办法
3971
查看次数

更改换行符.readline()寻求

是否可以.readline()在读取行时更改方法查找的换行符?我可能需要从文件对象中读取一个流,该文件对象将以换行符之外的其他内容分隔,并且一次获取一个块可能很方便. 如果我可以使用,那么file对象没有readuntil我不必创建的对象readline

编辑:


我还没有在管道上试过它stdin; 但这似乎有效.

class cfile(file):
    def __init__(self, *args):
        file.__init__(self, *args)

    def readuntil(self, char):
        buf = bytearray()
        while True:
            rchar = self.read(1)
            buf += rchar
            if rchar == char:
                return str(buf)
Run Code Online (Sandbox Code Playgroud)

用法:

>>> import test
>>> tfile = test.cfile('/proc/self/fd/0', 'r')
>>> tfile.readuntil('0')
this line has no char zero
this one doesn't either,
this one does though, 0
"this line has no char zero\nthis one doesn't either,\nthis one does though, 0" …
Run Code Online (Sandbox Code Playgroud)

python input readline

6
推荐指数
1
解决办法
2856
查看次数

pythons'print'语句不调用.write()方法?

我认为该print语句只是在sys.stdout(默认情况下)对象上调用了.write()方法.

但是写了一个像这样的子类:

import time

class logfile(file):
    def __init__(self, *args, **kwargs):
        file.__init__(self, *args, **kwargs)

    def write(self, logstr):
        if logstr[-1] != '\n': logstr += '\n'
        super(logfile, self).write(time.strftime('%D-%T ') + str(logstr))
Run Code Online (Sandbox Code Playgroud)

它似乎工作,如果我创建一个logfile对象并调用该write方法,但当试图将sys.stdout对象更改为一个实例时,logfile它似乎print没有调用write.也许writelines

使用这个:

#!/usr/bin/python

from myfile import logfile
import sys

sys.stdout = logfile('somefile', 'w')

print 'this is a test'
sys.stdout.write('this is another test')
Run Code Online (Sandbox Code Playgroud)

我的输出文件'somefile'包含:

this is a test
08/10/11-16:59:47 this is another test
Run Code Online (Sandbox Code Playgroud)

您可以看到输出文件中的第一行是我尝试过的,print …

python printing oop inheritance file

6
推荐指数
1
解决办法
173
查看次数

Python 字符串 FIFO

Python 有用于字符串 FIFO 缓冲的数据类型吗?我创建了一些东西(如下),但怀疑我正在重新发明轮子。

class Buffer(list):
    def __init__(self):
        super(Buffer, self).__init__()

    def put(self, nlmsg):
        for c in nlmsg: self.append(c)

    def peek(self, number):
        return "".join( [self[i] for i in range(number)] )

    def get(self, number):
        return "".join( [self.pop(0) for i in range(number)] )
Run Code Online (Sandbox Code Playgroud)

使用示例:

>>> buf = Buffer()
>>> buf.put('abcdefg')
>>> buf
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> buf.peek(4)
'abcd'
>>> buf
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> buf.get(5)
'abcde'
>>> buf
['f', 'g']
Run Code Online (Sandbox Code Playgroud)

我看了看,Queue但是当添加一个 str 时,我必须手动分割每个字节,否则整个 …

python string buffer

6
推荐指数
1
解决办法
4404
查看次数

Linux NASM检测EOF

我正在尝试在linux上学习基础知识,我找不到一个很好的参考.NASM文档似乎假设您已经知道masm ...我在cmp(英特尔指令参考之外)的文档中找不到任何示例.

我编写了一个从stdin读取单个字节并将其写入stdout的程序.下面是我的修改,尝试在stdin上检测EOF并在达到EOF时退出.问题是它永远不会退出.我只是继续打印从stdin读取的最后一个char.问题是在我的EOF检测(cmp ecx, EOF)和/或我跳到_exit标签(je _exit)我认为.

我究竟做错了什么?

%define EOF     -1

section .bss
        char:   resb    1

section .text
        global  _start

_exit:
        mov     eax,    1       ; exit
        mov     ebx,    0       ; exit status
        int     80h

_start:
        mov     eax,    3       ; sys_read
        mov     ebx,    0       ; stdin
        mov     ecx,    char    ; buffer
        cmp     ecx,    EOF     ; EOF?
        je      _exit
        mov     edx,    1       ; read byte count
        int     80h

        mov     eax,    4       ; sys_write
        mov     ebx, …
Run Code Online (Sandbox Code Playgroud)

linux assembly stdin nasm eof

6
推荐指数
1
解决办法
5430
查看次数

在python中睡觉准确的时间

我需要在其中一个函数中等待大约25ms.有时这个函数在处理器被其他东西占用时被调用,而有时它会将处理器全部用于自身.

我试过time.sleep(.25)但有时它实际上是25毫秒,有时它需要更长的时间.无论处理器可用性如何,有没有办法在一段时间内睡眠?

python windows sleep

6
推荐指数
5
解决办法
5789
查看次数

cherrypy/dev/urandom(或等效)未找到 - 错误

我运行的CherryPy 3.2.0服务器与Python 2.5.1,它提供了以下错误每隔几天从UI任何指令,直至被杀害,并且重新开始: -

[29/Mar/2012:06:37:57] HTTP Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/_cprequest.py", line 636, in respond
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/_cprequest.py", line 97, in run
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/_cprequest.py", line 57, in __call__
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/lib/sessions.py", line 757, in init
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/lib/sessions.py", line 162, in __init__
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/lib/sessions.py", line 190, in _regenerate
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/lib/sessions.py", line 204, in generate_id
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/_cpcompat.py", line 264, in random20
File "/usr/lib/python2.5/os.py", line 733, in urandom
NotImplementedError: /dev/urandom (or equivalent) not found
Run Code Online (Sandbox Code Playgroud)

_cpcompat.py有一段代码表明,random.random如果cherrypy无法读取/dev/urandom,但似乎没有倒退,则会出现回落.

try:
    os.urandom(20)
    import …
Run Code Online (Sandbox Code Playgroud)

python random session cherrypy

5
推荐指数
1
解决办法
3085
查看次数