简体   繁体   English

使用 certutil 和 Python 下载文件

[英]Downloading file using certutil and Python

I'm trying to download file by calling cmd command through Python.我正在尝试通过 Python 调用 cmd 命令来下载文件。 When I run this command in cmd:当我在 cmd 中运行此命令时:

certutil -urlcache -split -f https://www.contextures.com/SampleData.zip c:\temp\test.zip

The file downloads without any issues, but when I run that command through Python, the file is not being downloaded.文件下载没有任何问题,但是当我通过 Python 运行该命令时,文件没有被下载。 I tried:我试过:

import subprocess
command = "certutil -urlcache -split -f https://www.contextures.com/SampleData.zip c:\temp\test.zip"

subprocess.Popen([command])
subprocess.call(command, shell=True)

also:还:

os.system(command)

Any ideas why this doesn't work?任何想法为什么这不起作用? Any help would be greatly appreciated.任何帮助将不胜感激。

Thanks!谢谢!

First: problem can makes \\t which has special meaning in Python (and other languages) and you should use "c:\\\\temp\\\\test.zip" or you would have to use prefix r to create raw string r"c:\\temp\\test.zip"第一:问题可以使\\t在 Python(和其他语言)中具有特殊含义,您应该使用"c:\\\\temp\\\\test.zip"或者您必须使用前缀r来创建原始字符串r"c:\\temp\\test.zip"

Second: when you don't use shell=True then you need list like第二:当你不使用shell=True那么你需要像这样的列表

["certutil", "-urlcache", "-split", "-f", "https://www.contextures.com/SampleData.zip", "c:\\temp\\test.zip"]

Sometimes people simply use split(' ') to create it有时人们只是使用split(' ')来创建它

"certutil -urlcache -split -f https://www.contextures.com/SampleData.zip c:\\temp\\test.zip".split(" ")

And then you can test both versions然后你可以测试两个版本

cmd = "certutil -urlcache -split -f https://www.contextures.com/SampleData.zip c:\\temp\\test.zip"

Popen(cmd.split(' '))

Popen(cmd, shell=True)

EDIT:编辑:

If you will have more complex command - ie.如果你有更复杂的命令 - 即。 with " " inside string - then you can use standard module shlex and command shlex.split(cmd) .使用" "字符串 - 然后您可以使用标准模块shlex和命令shlex.split(cmd) To keep \\\\ in path you may need `posix=False要将\\\\保留在路径中,您可能需要 `posix=False

import shlex

cmd = "certutil -urlcache -split -f https://www.contextures.com/SampleData.zip c:\\temp\\test.zip"

Popen(shlex.split(cmd, posix=False))

For example this gives incorrect list with 4 elements例如,这给出了包含 4 个元素的错误列表

'--text "hello world" --other'.split(' ')

['--text', '"hello', 'world"', '--other']

but this gives correct list with 3 elements但这给出了包含 3 个元素的正确列表

shlex.split('--text "hello world" --other')

['--text', 'hello world', '--other']

Also, a raw string can be specified that will not interpret escape sequences such as \\t .此外,可以指定一个raw字符串,它不会解释转义序列,例如\\t

Python 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:46:45) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("now\time")
now     ime
>>> print(r"now\time")
now\time
>>> print('now\time')
now     ime
>>> print(r'now\time')
now\time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM