我需要使用带有python 子进程模块的标准UNIX diff命令创建一个diff文件.问题是我必须比较文件和流而不创建tempopary文件.我想过通过os.mkfifo方法使用命名管道,但没有达到任何好结果.请问,你能写一个关于如何解决这个问题的简单例子吗?我试过这样:
fifo = 'pipe'
os.mkfifo(fifo)
op = popen('cat ', fifo)
print >> open(fifo, 'w'), output
os.unlink(fifo)
proc = Popen(['diff', '-u', dumpfile], stdin=op, stdout=PIPE)
Run Code Online (Sandbox Code Playgroud)
但似乎diff没有看到第二个论点.
我正在为用户注册编写自己的验证码系统.所以我需要创建一个合适的URL来接收生成的验证码图片.一代看起来像这样:
_cipher = cipher.new(settings.CAPTCHA_SECRET_KEY, cipher.MODE_ECB)
_encrypt_block = lambda block: _cipher.encrypt(block + ' ' * (_cipher.block_size - len(block) % _cipher.block_size))
#...
a = (self.rightnum, self.animal_type[1])
serialized = pickle.dumps(a)
encrypted = _encrypt_block(serialized)
safe_url = urlsafe_b64encode(encrypted)
Run Code Online (Sandbox Code Playgroud)
但后来我试图通过视图函数中的GET请求接收此密钥,它在urlsafe_b64decode()上失败,"字符映射必须返回整数,无或unicode"错误:
def captcha(request):
try:
key = request.REQUEST['key']
decoded = urlsafe_b64decode(key)
decrypted = _decrypt_block(decoded)
deserialized = pickle.loads(decrypted)
return HttpResponse(deserialized)
except KeyError:
return HttpResponseBadRequest()
Run Code Online (Sandbox Code Playgroud)
我发现在urlsafe_b64encode的输出上有一个str,但是GET请求返回一个unicode对象(不过它是一个正确的字符串).Str()没有帮助(它在django内部返回解码错误),如果我使用密钥.repr它工作,但解密器不能使用错误"输入字符串必须是16的倍数".在测试文件里面所有这些结构都很完美,我无法理解,有什么不对?
我正在使用raw_input()以交互模式从用户接收密码,但出于安全原因,我想使输入符号不可见,就像您使用sudo键入密码或连接到数据库时一样.我应该怎么做?
我正在尝试在django管理面板中创建一个"酷"图像上传界面.问题是我使用内联接口上传图像实例,但我使用jQuery Ajax表单上传图像,因此我需要创建另一种表格进行上传.而现在django视图函数无法从这种形式接收request.FILES,因为我在不使用django.forms的情况下创建了它,并且无法在视图函数中指定需要使用哪种形式.所以我不能覆盖内联接口的标准视图函数,也不能使用django.forms重新创建这个表单.所以这段代码似乎不起作用:
我的表格:
<form id="uploadForm" enctype="multipart/form-data" method="post" action="upload_picture/">
<input type="file" name="file" />
<input type="submit" value="Upload" />
</form>
Run Code Online (Sandbox Code Playgroud)
我的观点功能:
def upload_picture(request):
if request.method == 'POST':
save_original(request.FILES['file'])
return HttpResponseRedirect('admin/edit_inline/picture_editor.html')
Run Code Online (Sandbox Code Playgroud)
也许我应该完全不同的方式?
我有两个软件包和一个virtualenv。而且,我正在尝试将它们都以可编辑模式安装到此virtualenv中,这意味着“ python setup.py开发 ”或“ pip install -e ”。此过程通常导致三件事:
因此,我在步骤2中遇到了问题。根据安装顺序,如果我先安装软件包A,然后再安装软件包B(使用相同的pip install -e),则一切正常,并且两个路径都在easy-install.pth中。但是否则,如果我先安装软件包B,然后再安装软件包A-第二个覆盖了easy-install.pth,则那里只有一个路径-B 。
程序包A和B彼此之间没有任何依赖关系。点子有什么问题?
我正在学习如何在Linux中使用原始套接字.我正在尝试创建一个这样的套接字:
if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
perror("socket() failed");
exit(-1);
}
Run Code Online (Sandbox Code Playgroud)
但我在发布后获得的是:
socket()失败:不允许操作
我知道只有root可以创建原始套接字,但如果我用SUID位或sudo运行它 - 问题是一样的.怎么了?该系统是Ubuntu 11.04.
也许我包括不必要的标题?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
Run Code Online (Sandbox Code Playgroud)
我想知道 - 为什么SUID没用?
我在某些服务器上有一个巨大的 HTML 文件(数十兆字节),我需要定期下载和解析该文件,检测更改。因此,我尝试使用最常用的工具来完成此任务 - requests和lxml。
我发现的流解析的常见方法与此类似:
def fast_iter(url):
resp = requests.get(
url,
stream=True
)
context = etree.iterparse(resp.raw, html=True)
for event, elem in context:
print(elem)
if event == 'end' and elem.tag in TAGS:
yield elem
elem.clear()
while elem.getprevious() is not None:
if elem.getparent():
del elem.getparent()[0]
else:
break
del context
Run Code Online (Sandbox Code Playgroud)
但在我的例子中,它不起作用,因为iterparse()变得疯狂并返回一些从未出现在源 HTML 文件中的元素(并且它没有损坏!):
<Element vqoe at 0x7eff9762b448>
<Element jzu at 0x7eff9762b408>
<Element vvu at 0x7eff9762b3c8>
<Element d at 0x7eff9762b388>
<Element s at 0x7eff9762b348>
<Element ss_lt at 0x7eff9762b308> …Run Code Online (Sandbox Code Playgroud) 我需要接收有关修订,文件更改和更改代码行数的数据而不克隆回购,我唯一拥有的是repo url.我发现用于查看远程更改的唯一命令是git ls remote,但它的输出太差了.我怎样才能做到这一点?
我试图patch在stdout上捕获bash 的输出,但是我收到一个错误:
patch -o- some/file
patch: can't output patches to standard output
Run Code Online (Sandbox Code Playgroud)
我可以在stdout上获得补丁结果吗?