Hướng dẫn python subprocess parse output - đầu ra phân tích cú pháp của quy trình con python

Tôi là một dịch vụ web từ mã Python của mình:

response = subprocess.call(['curl', '-k', '-i', '-H' , 'content-type: application/soap+xml' ,'-d',  etree.tostring(tree), '-v' ,'https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1'])

Dịch vụ trả về một tin nhắn SOAP, làm cách nào để phân tích thông báo SOAP và tìm hiểu xem đó là một thất bại hay thành công?

Tôi đã cố gắng sử dụng những điều sau đây nhưng tôi nhận được kết quả sai:

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

Có thể đặt những thứ này thành sai trên bất kỳ phiên bản Python nào. Chúng sẽ không có tác dụng đối với các phiên bản cũ hơn khi không được hỗ trợ. Không cho rằng các thuộc tính có sẵn để đọc. Mặc dù tên của họ, một giá trị thực sự không chỉ ra rằng hàm tương ứng sẽ được sử dụng, chỉ có điều đó có thể. Lib/subprocess.py


Vui lòng giải quyết các vấn đề bất cứ khi nào bạn phải sử dụng các nút riêng này với cách tái tạo vấn đề bạn đang thấy. Liên kết đến vấn đề đó từ một bình luận trong mã của bạn.

Mới trong phiên bản 3.8:

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
09

Một chuỗi được bao quanh bởi các dấu ngoặc kép được hiểu là một đối số duy nhất, bất kể không gian trắng có trong. Một chuỗi được trích dẫn có thể được nhúng trong một đối số.

Một dấu ngoặc kép có trước một dấu gạch chéo ngược được hiểu là một dấu ngoặc kép theo nghĩa đen. – PEP proposing the subprocess module

Backslashes được giải thích theo nghĩa đen, trừ khi chúng ngay lập tức đi trước dấu ngoặc kép.: not Emscripten, not WASI.

Nếu dấu gạch chéo ngược ngay trước dấu ngoặc kép, mỗi cặp dấu gạch chéo ngược được hiểu là một dấu gạch chéo ngược theo nghĩa đen. Nếu số lượng dấu gạch chéo ngược là số lẻ, dấu gạch chéo ngược cuối cùng sẽ thoát khỏi dấu ngoặc kép tiếp theo như được mô tả trong Quy tắc 3.WebAssembly platforms for more information.

Xem thêm

Mô -đun cung cấp chức năng cho các dòng lệnh phân tích và thoát.

Vô hiệu hóa việc sử dụng
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
99 hoặc ________ 400¶
run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)

Trên Linux,

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
0 mặc định sử dụng hệ thống
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
99 gọi nội bộ khi an toàn để làm như vậy thay vì
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
03. Điều này cải thiện đáng kể hiệu suất.

Các đối số được hiển thị ở trên chỉ là những đối số phổ biến nhất, được mô tả dưới đây trong các đối số được sử dụng thường xuyên (do đó việc sử dụng ký hiệu chỉ từ khóa trong chữ ký viết tắt). Chữ ký chức năng đầy đủ phần lớn giống như hàm xây dựng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 - hầu hết các đối số cho hàm này được chuyển qua giao diện đó. (Thời gian chờ, đầu vào, kiểm tra và capture_output không.)Frequently Used Arguments (hence the use of keyword-only notation in the abbreviated signature). The full function signature is largely the same as that of the
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 constructor - most of the arguments to this function are passed through to that interface. (timeout, input, check, and capture_output are not.)

Nếu Capture_output là đúng, stdout và stderr sẽ bị bắt. Khi được sử dụng, đối tượng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 bên trong được tự động tạo bằng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 và
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1. Các đối số stdout và stderr có thể không được cung cấp cùng lúc với Capture_output. Nếu bạn muốn chụp và kết hợp cả hai luồng thành một, hãy sử dụng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 và
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
3 thay vì Capture_output.

Đối số thời gian chờ được chuyển đến

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
4. Nếu thời gian chờ hết hạn, quá trình trẻ em sẽ bị giết và chờ đợi. Ngoại lệ
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
5 sẽ được nuôi dưỡng lại sau khi quá trình con đã chấm dứt.

Đối số đầu vào được chuyển đến

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
4 và do đó cho quá trình phụ stdin. Nếu được sử dụng, nó phải là một chuỗi byte hoặc một chuỗi nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản là đúng. Khi được sử dụng, đối tượng
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 bên trong được tự động tạo bằng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 và đối số Stdin cũng không được sử dụng.

Nếu kiểm tra là đúng và quy trình thoát ra với mã thoát khác không, ngoại lệ

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 sẽ được nêu ra. Các thuộc tính của ngoại lệ đó giữ các đối số, mã thoát và stdout và stderr nếu chúng bị bắt.

Nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản là đúng, các đối tượng tệp cho stdin, stdout và stderr được mở ở chế độ văn bản bằng cách sử dụng mã hóa và lỗi được chỉ định hoặc mặc định

Popen(['/bin/sh', '-c', args[0], args[1], ...])
0. Đối số Universal_Newlines tương đương với văn bản và được cung cấp cho khả năng tương thích ngược. Theo mặc định, các đối tượng tệp được mở ở chế độ nhị phân.

Nếu env không phải là

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, thì đó phải là một ánh xạ xác định các biến môi trường cho quy trình mới; Chúng được sử dụng thay vì hành vi mặc định của việc kế thừa môi trường quy trình hiện tại. Nó được truyền trực tiếp đến
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6.

Examples:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')

Mới trong phiên bản 3.5.

Đã thay đổi trong phiên bản 3.6: Đã thêm thông số mã hóa và lỗiAdded encoding and errors parameters

Đã thay đổi trong phiên bản 3.7: Đã thêm tham số văn bản, như một bí danh dễ hiểu hơn của Universal_Newlines. Đã thêm tham số Capture_output.Added the text parameter, as a more understandable alias of universal_newlines. Added the capture_output parameter.

ClassSubProcess.completedProcess¶ subprocess.CompletedProcess

Giá trị trả về từ

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5, đại diện cho một quá trình đã kết thúc.

args¶

Các đối số được sử dụng để khởi chạy quá trình. Đây có thể là một danh sách hoặc một chuỗi.

ReturnCode¶

Trạng thái thoát của quá trình trẻ em. Thông thường, trạng thái thoát là 0 chỉ ra rằng nó đã chạy thành công.

Giá trị âm

Popen(['/bin/sh', '-c', args[0], args[1], ...])
4 chỉ ra rằng đứa trẻ bị chấm dứt bởi tín hiệu
Popen(['/bin/sh', '-c', args[0], args[1], ...])
5 (chỉ POSIX).

Stdout¶

Đã bắt được stdout từ quá trình trẻ em. Trình tự byte hoặc chuỗi nếu

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 được gọi với mã hóa, lỗi hoặc văn bản = true.
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 nếu stdout không bị bắt.

Nếu bạn chạy quy trình với

Popen(['/bin/sh', '-c', args[0], args[1], ...])
8, stdout và stderr sẽ được kết hợp trong thuộc tính này và
Popen(['/bin/sh', '-c', args[0], args[1], ...])
9 sẽ là
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1.

stderr¶

Đã bắt Stderr từ quá trình trẻ em. Trình tự byte hoặc chuỗi nếu

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 được gọi với mã hóa, lỗi hoặc văn bản = true.
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 nếu Stderr không bị bắt.

Check_returnCode ()()

Nếu

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3 là không khác, hãy tăng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9.

Mới trong phiên bản 3.5.

Đã thay đổi trong phiên bản 3.6: Đã thêm thông số mã hóa và lỗiDEVNULL

Đã thay đổi trong phiên bản 3.7: Đã thêm tham số văn bản, như một bí danh dễ hiểu hơn của Universal_Newlines. Đã thêm tham số Capture_output.

ClassSubProcess.completedProcess¶

Giá trị trả về từ
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5, đại diện cho một quá trình đã kết thúc.
PIPE

args¶

Các đối số được sử dụng để khởi chạy quá trình. Đây có thể là một danh sách hoặc một chuỗi.STDOUT

ReturnCode¶

Trạng thái thoát của quá trình trẻ em. Thông thường, trạng thái thoát là 0 chỉ ra rằng nó đã chạy thành công. subprocess.SubprocessError

Giá trị âm

Popen(['/bin/sh', '-c', args[0], args[1], ...])
4 chỉ ra rằng đứa trẻ bị chấm dứt bởi tín hiệu
Popen(['/bin/sh', '-c', args[0], args[1], ...])
5 (chỉ POSIX).

ClassSubProcess.completedProcess¶

Giá trị trả về từ
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5, đại diện cho một quá trình đã kết thúc.
subprocess.TimeoutExpired

args¶

Các đối số được sử dụng để khởi chạy quá trình. Đây có thể là một danh sách hoặc một chuỗi.

ReturnCode¶

Trạng thái thoát của quá trình trẻ em. Thông thường, trạng thái thoát là 0 chỉ ra rằng nó đã chạy thành công.

Giá trị âm

Popen(['/bin/sh', '-c', args[0], args[1], ...])
4 chỉ ra rằng đứa trẻ bị chấm dứt bởi tín hiệu
Popen(['/bin/sh', '-c', args[0], args[1], ...])
5 (chỉ POSIX).

Stdout¶

Đã bắt được stdout từ quá trình trẻ em. Trình tự byte hoặc chuỗi nếu

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 được gọi với mã hóa, lỗi hoặc văn bản = true.
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 nếu stdout không bị bắt.

Stdout¶

Đã bắt được stdout từ quá trình trẻ em. Trình tự byte hoặc chuỗi nếu

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 được gọi với mã hóa, lỗi hoặc văn bản = true.
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 nếu stdout không bị bắt.

stderr¶

Đã bắt Stderr từ quá trình trẻ em. Trình tự byte hoặc chuỗi nếu

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 được gọi với mã hóa, lỗi hoặc văn bản = true.
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 nếu Stderr không bị bắt.

Mới trong phiên bản 3.3.

Đã thay đổi trong Phiên bản 3.5: Thuộc tính Stdout và Stderr được thêmstdout and stderr attributes added

ngoại lệ subprocess.CalledProcessError

Lớp con của

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
0, được nêu ra khi một quy trình chạy bằng
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
6,
proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
2 hoặc
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 (với
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
9) trả về trạng thái thoát khác không.

ReturnCode¶

Trạng thái thoát của quá trình trẻ em. Nếu quá trình thoát ra do tín hiệu, đây sẽ là số tín hiệu âm.

cmd¶

Lệnh được sử dụng để sinh ra quá trình trẻ em.

đầu ra

Đầu ra của quá trình con nếu nó bị bắt bởi

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 hoặc
proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
2. Nếu không,
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1.

Stdout¶

Bí danh cho đầu ra, để đối xứng với

Popen(['/bin/sh', '-c', args[0], args[1], ...])
9.

stderr¶

Đầu ra Stderr của quá trình con nếu nó bị bắt bởi

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5. Nếu không,
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1.

Đã thay đổi trong Phiên bản 3.5: Thuộc tính Stdout và Stderr được thêmstdout and stderr attributes added

Các đối số thường được sử dụng

Để hỗ trợ nhiều trường hợp sử dụng khác nhau, hàm tạo

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 (và các hàm tiện lợi) chấp nhận một số lượng lớn các đối số tùy chọn. Đối với hầu hết các trường hợp sử dụng điển hình, nhiều đối số này có thể được để lại một cách an toàn tại các giá trị mặc định của chúng. Các đối số phổ biến nhất là:

Args là bắt buộc cho tất cả các cuộc gọi và phải là một chuỗi, hoặc một chuỗi các đối số chương trình. Cung cấp một chuỗi các đối số thường được ưa thích, vì nó cho phép mô -đun chăm sóc bất kỳ việc thoát ra và trích dẫn các đối số cần thiết nào (ví dụ: cho phép không gian trong tên tệp). Nếu truyền một chuỗi duy nhất, một trong hai shell phải là

run(..., check=True, stdout=PIPE).stdout
7 (xem bên dưới) hoặc nếu không chuỗi phải chỉ cần đặt tên cho chương trình sẽ được thực thi mà không cần chỉ định bất kỳ đối số nào.

Stdin, stdout và stderr chỉ định đầu vào tiêu chuẩn của chương trình, đầu ra tiêu chuẩn và tay cầm tệp tiêu chuẩn, tương ứng. Các giá trị hợp lệ là

run(..., check=True, stdout=PIPE).stdout
8,
run(..., check=True, stdout=PIPE).stdout
9, một bộ mô tả tệp hiện có (một số nguyên dương), một đối tượng tệp hiện có với bộ mô tả tệp hợp lệ và
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1.
run(..., check=True, stdout=PIPE).stdout
8 chỉ ra rằng một đường ống mới cho trẻ nên được tạo ra.
run(..., check=True, stdout=PIPE).stdout
9 chỉ ra rằng tệp đặc biệt
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
6 sẽ được sử dụng. Với các cài đặt mặc định của
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, sẽ không có sự chuyển hướng nào xảy ra; Tay cầm tập tin trẻ con sẽ được kế thừa từ cha mẹ. Ngoài ra, STDERR có thể là
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
05, điều này chỉ ra rằng dữ liệu STDERR từ quy trình con phải được ghi vào cùng một tay cầm tệp như đối với stdout.

Nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản (còn được gọi là Universal_Newlines) là đúng, các đối tượng tệp STDIN, STDOUT và STDERR sẽ được mở ở chế độ văn bản bằng cách sử dụng mã hóa và lỗi được chỉ định trong cuộc gọi hoặc mặc định cho

Popen(['/bin/sh', '-c', args[0], args[1], ...])
0.

Đối với STDIN, các ký tự kết thúc dòng

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
07 trong đầu vào sẽ được chuyển đổi thành dấu phân cách dòng mặc định
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
08. Đối với stdout và stderr, tất cả các kết thúc dòng trong đầu ra sẽ được chuyển đổi thành
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
07. Để biết thêm thông tin, hãy xem tài liệu của lớp
Popen(['/bin/sh', '-c', args[0], args[1], ...])
0 khi đối số dòng mới cho hàm tạo của nó là
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1.

Nếu chế độ văn bản không được sử dụng, stdin, stdout và stderr sẽ được mở dưới dạng các luồng nhị phân. Không có chuyển đổi mã hóa hoặc kết thúc dòng được thực hiện.

Mới trong phiên bản 3.6: Đã thêm các tham số mã hóa và lỗi.Added encoding and errors parameters.

Mới trong phiên bản 3.7: Đã thêm tham số văn bản dưới dạng bí danh cho Universal_Newlines.Added the text parameter as an alias for universal_newlines.

Nếu shell là

run(..., check=True, stdout=PIPE).stdout
7, lệnh được chỉ định sẽ được thực thi thông qua shell. Điều này có thể hữu ích nếu bạn đang sử dụng Python chủ yếu cho luồng điều khiển nâng cao mà nó cung cấp trên hầu hết các vỏ hệ thống và vẫn muốn truy cập thuận tiện vào các tính năng vỏ khác như ống shell, ký tự đại diện, mở rộng biến đổi môi trường và mở rộng
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
13 cho nhà của người dùng danh mục. Tuy nhiên, lưu ý rằng bản thân Python cung cấp việc triển khai nhiều tính năng giống như vỏ (đặc biệt là
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
14,
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
15,
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
16,
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
17,
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
18 và
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
19).

Đã thay đổi trong phiên bản 3.3: Khi Universal_Newlines là

run(..., check=True, stdout=PIPE).stdout
7, lớp sử dụng mã hóa
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
21 thay vì
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
22. Xem lớp
Popen(['/bin/sh', '-c', args[0], args[1], ...])
0 để biết thêm thông tin về thay đổi này.When universal_newlines is
run(..., check=True, stdout=PIPE).stdout
7, the class uses the encoding
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
21 instead of
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
22. See the
Popen(['/bin/sh', '-c', args[0], args[1], ...])
0 class for more information on this change.

Các tùy chọn này, cùng với tất cả các tùy chọn khác, được mô tả chi tiết hơn trong tài liệu xây dựng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6.

Constructor Popen Constructor¶

Việc tạo và quản lý quy trình cơ bản trong mô -đun này được xử lý bởi lớp

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6. Nó cung cấp rất nhiều sự linh hoạt để các nhà phát triển có thể xử lý các trường hợp ít phổ biến hơn không được bao phủ bởi các chức năng tiện lợi.

classSubProcess.popen (args, bufsize = -1, thực thi = none, stdin = none, stdout = none, stderr = none . = None, pipesize = -1, process_group = none) ¶subprocess.Popen(args, bufsize=- 1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, group=None, extra_groups=None, user=None, umask=- 1, encoding=None, errors=None, text=None, pipesize=- 1, process_group=None)

Thực hiện một chương trình trẻ em trong một quy trình mới. Trên Posix, lớp sử dụng hành vi giống như ____ 126 để thực hiện chương trình trẻ em. Trên Windows, lớp sử dụng chức năng Windows

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
27. Các đối số cho
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 như sau.

Args nên là một chuỗi các đối số chương trình hoặc một đối tượng giống như một chuỗi hoặc giống như đường dẫn. Theo mặc định, chương trình thực thi là mục đầu tiên trong args nếu args là một chuỗi. Nếu Args là một chuỗi, việc giải thích phụ thuộc vào nền tảng và được mô tả dưới đây. Xem các đối số shell và thực thi để biết sự khác biệt bổ sung so với hành vi mặc định. Trừ khi có quy định khác, nên truyền ARGS như một chuỗi.path-like object. By default, the program to execute is the first item in args if args is a sequence. If args is a string, the interpretation is platform-dependent and described below. See the shell and executable arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to pass args as a sequence.

Cảnh báo

Để có độ tin cậy tối đa, hãy sử dụng một đường dẫn đủ điều kiện cho thực thi. Để tìm kiếm một tên không đủ tiêu chuẩn trên

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
29, hãy sử dụng
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
30. Trên tất cả các nền tảng, việc vượt qua
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
31 là cách được đề xuất để khởi chạy lại trình thông dịch Python hiện tại và sử dụng định dạng dòng lệnh
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
32 để khởi chạy mô-đun đã cài đặt.
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
29, use
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
30. On all platforms, passing
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
31 is the recommended way to launch the current Python interpreter again, and use the
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
32 command-line format to launch an installed module.

Việc giải quyết đường dẫn thực thi (hoặc mục đầu tiên của Args) phụ thuộc vào nền tảng. Đối với POSIX, xem

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
26 và lưu ý rằng khi giải quyết hoặc tìm kiếm đường dẫn thực thi, CWD ghi đè thư mục làm việc hiện tại và ENV có thể ghi đè biến môi trường
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
29. Đối với Windows, hãy xem tài liệu của các tham số
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
35 và
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
36 của WinAPI
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
37 và lưu ý rằng khi giải quyết hoặc tìm kiếm đường dẫn thực thi với
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
38, CWD không ghi đè lên thư mục làm việc hiện tại và Env không thể ghi đè lên biến đổi môi trường
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
29. Sử dụng một đường dẫn đầy đủ tránh tất cả các biến thể này.

Một ví dụ về việc chuyển một số đối số cho một chương trình bên ngoài như một chuỗi là:

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])

Trên Posix, nếu Args là một chuỗi, chuỗi được hiểu là tên hoặc đường dẫn của chương trình để thực thi. Tuy nhiên, điều này chỉ có thể được thực hiện nếu không chuyển các đối số cho chương trình.

Ghi chú

Có thể không rõ ràng làm thế nào để chia một lệnh shell thành một chuỗi các đối số, đặc biệt là trong các trường hợp phức tạp.

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
40 có thể minh họa cách xác định mã thông báo chính xác cho ARGS:

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!

Lưu ý cụ thể rằng các tùy chọn (chẳng hạn như -input) và các đối số (như trứng.txt) được phân tách bằng khoảng trắng trong shell đi trong các yếu tố danh sách riêng biệt, trong khi các đối số cần trích dẫn hoặc rút lại khi sử dụng Tên tệp chứa khoảng trắng hoặc lệnh Echo được hiển thị ở trên) là các yếu tố danh sách đơn.

Trên Windows, nếu Args là một chuỗi, nó sẽ được chuyển đổi thành một chuỗi theo cách được mô tả trong việc chuyển đổi một chuỗi đối số thành một chuỗi trên Windows. Điều này là do

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
27 cơ bản hoạt động trên các chuỗi.Converting an argument sequence to a string on Windows. This is because the underlying
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
27 operates on strings.

Đã thay đổi trong phiên bản 3.6: Tham số Args chấp nhận một đối tượng giống như đường dẫn nếu shell là

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
42 và một chuỗi chứa các đối tượng giống như đường dẫn trên POSIX.args parameter accepts a path-like object if shell is
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
42 and a sequence containing path-like objects on POSIX.

Thay đổi trong phiên bản 3.8: Tham số Args chấp nhận một đối tượng giống như đường dẫn nếu shell là

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
42 và một chuỗi chứa byte và các đối tượng giống như đường dẫn trên Windows.args parameter accepts a path-like object if shell is
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
42 and a sequence containing bytes and path-like objects on Windows.

Đối số shell (mặc định là

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
42) chỉ định xem có nên sử dụng shell làm chương trình để thực thi hay không. Nếu shell là
run(..., check=True, stdout=PIPE).stdout
7, nên chuyển Args dưới dạng chuỗi chứ không phải là một chuỗi.

Trên POSIX với

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
46, shell mặc định là
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
47. Nếu args là một chuỗi, chuỗi chỉ định lệnh để thực thi thông qua shell. Điều này có nghĩa là chuỗi phải được định dạng chính xác như khi được gõ tại dấu nhắc shell. Điều này bao gồm, ví dụ, trích dẫn hoặc chao đảo thoát khỏi các tên tệp với không gian trong đó. Nếu Args là một chuỗi, mục đầu tiên chỉ định chuỗi lệnh và bất kỳ mục bổ sung nào sẽ được coi là đối số bổ sung cho chính shell. Điều đó có nghĩa là,
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 có tương đương với:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

Trên Windows với

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
46, biến môi trường
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
50 chỉ định shell mặc định. Lần duy nhất bạn cần chỉ định
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
46 trên Windows là khi lệnh bạn muốn thực thi được tích hợp vào shell (ví dụ: dir hoặc sao chép). Bạn không cần
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
46 để chạy tệp hàng loạt hoặc thực thi dựa trên bảng điều khiển.
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
50 environment variable specifies the default shell. The only time you need to specify
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
46 on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
46 to run a batch file or console-based executable.

Bufsize sẽ được cung cấp dưới dạng đối số tương ứng cho hàm

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
53 khi tạo các đối tượng tệp ống stdin/stdout/stderr:

  • subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    
    54 có nghĩa là không bị ảnh hưởng (đọc và ghi là một cuộc gọi hệ thống và có thể quay lại ngắn)

  • subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    
    55 có nghĩa là bộ đệm dòng (chỉ có thể sử dụng nếu
    subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    
    56 tức là, trong chế độ văn bản)

  • Bất kỳ giá trị dương nào khác có nghĩa là sử dụng bộ đệm có kích thước xấp xỉ

  • Bufsize âm (mặc định) có nghĩa là mặc định hệ thống của io.default_buffer_size sẽ được sử dụng.

Đã thay đổi trong phiên bản 3.3.1: Bufsize hiện có mặc định thành -1 để bật bộ đệm theo mặc định để phù hợp với hành vi mà hầu hết mã mong đợi. Trong các phiên bản trước Python 3.2.4 và 3.3.1, nó được mặc định không chính xác là

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
54 được không bị truy cập và cho phép đọc ngắn. Điều này là vô ý và không phù hợp với hành vi của Python 2 như hầu hết các mã mong đợi.bufsize now defaults to -1 to enable buffering by default to match the behavior that most code expects. In versions prior to Python 3.2.4 and 3.3.1 it incorrectly defaulted to
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
54 which was unbuffered and allowed short reads. This was unintentional and did not match the behavior of Python 2 as most code expected.

Đối số thực thi chỉ định một chương trình thay thế để thực thi. Nó rất hiếm khi cần thiết. Khi

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
38, thực thi có thể thay thế chương trình để thực hiện được chỉ định bởi Args. Tuy nhiên, ARG ban đầu vẫn được chuyển cho chương trình. Hầu hết các chương trình coi chương trình được ARG chỉ định là tên lệnh, sau đó có thể khác với chương trình thực sự được thực hiện. Trên Posix, tên ARGS trở thành tên hiển thị cho các tiện ích có thể thực thi như PS. Nếu
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
46, trên POSIX đối số thực thi chỉ định một shell thay thế cho
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
47 mặc định.ps. If
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
46, on POSIX the executable argument specifies a replacement shell for the default
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
47.

Thay đổi trong phiên bản 3.6: Tham số thực thi chấp nhận một đối tượng giống như đường dẫn trên POSIX.executable parameter accepts a path-like object on POSIX.

Thay đổi trong phiên bản 3.8: Tham số thực thi chấp nhận một byte và đối tượng giống như đường dẫn trên Windows.executable parameter accepts a bytes and path-like object on Windows.

Stdin, stdout và stderr chỉ định đầu vào tiêu chuẩn của chương trình, đầu ra tiêu chuẩn và tay cầm tệp tiêu chuẩn, tương ứng. Các giá trị hợp lệ là

run(..., check=True, stdout=PIPE).stdout
8,
run(..., check=True, stdout=PIPE).stdout
9, một bộ mô tả tệp hiện có (một số nguyên dương), một đối tượng tệp hiện có với bộ mô tả tệp hợp lệ và
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1.
run(..., check=True, stdout=PIPE).stdout
8 chỉ ra rằng một đường ống mới cho trẻ nên được tạo ra.
run(..., check=True, stdout=PIPE).stdout
9 chỉ ra rằng tệp đặc biệt
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
6 sẽ được sử dụng. Với các cài đặt mặc định của
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, sẽ không có sự chuyển hướng nào xảy ra; Tay cầm tập tin trẻ con sẽ được kế thừa từ cha mẹ. Ngoài ra, STDERR có thể là
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
05, điều này chỉ ra rằng dữ liệu STDERR từ các ứng dụng phải được ghi vào cùng một tay cầm tệp như đối với stdout.file object with a valid file descriptor, and
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1.
run(..., check=True, stdout=PIPE).stdout
8 indicates that a new pipe to the child should be created.
run(..., check=True, stdout=PIPE).stdout
9 indicates that the special file
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
6 will be used. With the default settings of
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, no redirection will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can be
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
05, which indicates that the stderr data from the applications should be captured into the same file handle as for stdout.

Nếu preexec_fn được đặt thành một đối tượng có thể gọi được, đối tượng này sẽ được gọi trong quá trình con ngay trước khi đứa trẻ được thực thi. (Chỉ Posix)

Cảnh báo

Tham số PEREXEC_FN không an toàn để sử dụng với sự hiện diện của các luồng trong ứng dụng của bạn. Quá trình trẻ em có thể bế tắc trước khi EXEC được gọi.

Ghi chú

Nếu bạn cần sửa đổi môi trường cho đứa trẻ sử dụng tham số env thay vì thực hiện nó trong preexec_fn. Các tham số start_new_session và process_group sẽ thay thế mã bằng cách sử dụng preexec_fn để gọi

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
69 hoặc
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
70 ở trẻ.

Đã thay đổi trong phiên bản 3.8: Tham số PreExec_FN không còn được hỗ trợ trong các phần phụ. Việc sử dụng tham số trong trình điều khiển phụ tăng

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
71. Hạn chế mới có thể ảnh hưởng đến các ứng dụng được triển khai trong MOD_WSGI, UWSGI và các môi trường nhúng khác.The preexec_fn parameter is no longer supported in subinterpreters. The use of the parameter in a subinterpreter raises
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
71. The new restriction may affect applications that are deployed in mod_wsgi, uWSGI, and other embedded environments.

Nếu Close_FDS là đúng, tất cả các mô tả tệp ngoại trừ

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
54,
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
55 và
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
74 sẽ được đóng trước khi quá trình con được thực thi. Mặt khác, khi Close_FDS là sai, các mô tả tệp tuân theo cờ di truyền của họ như được mô tả trong kế thừa của các mô tả tệp.Inheritance of File Descriptors.

Trên Windows, nếu Close_FDS là đúng thì sẽ không có tay cầm nào được kế thừa bởi quy trình con trừ khi được thông qua rõ ràng trong phần tử

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
75 của
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
76 hoặc theo chuyển hướng xử lý tiêu chuẩn.

Đã thay đổi trong phiên bản 3.2: Mặc định cho Close_FDS đã được thay đổi từ

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
42 thành những gì được mô tả ở trên.The default for close_fds was changed from
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
42 to what is described above.

Đã thay đổi trong phiên bản 3.7: Trên Windows, mặc định cho Close_FDS đã được thay đổi từ

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
42 thành
run(..., check=True, stdout=PIPE).stdout
7 khi chuyển hướng tay cầm tiêu chuẩn. Bây giờ, nó có thể đặt Close_fds thành
run(..., check=True, stdout=PIPE).stdout
7 khi chuyển hướng các tay cầm tiêu chuẩn.On Windows the default for close_fds was changed from
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
42 to
run(..., check=True, stdout=PIPE).stdout
7 when redirecting the standard handles. It’s now possible to set close_fds to
run(..., check=True, stdout=PIPE).stdout
7 when redirecting the standard handles.

pass_fds là một chuỗi các mô tả tệp tùy chọn để tiếp tục mở giữa cha mẹ và con cái. Cung cấp bất kỳ lực lượng pass_fds gần

run(..., check=True, stdout=PIPE).stdout
7. (Chỉ Posix)

Đã thay đổi trong phiên bản 3.2: Tham số pass_fds đã được thêm vào.The pass_fds parameter was added.

Nếu CWD không phải là

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, chức năng sẽ thay đổi thư mục làm việc thành CWD trước khi thực hiện trẻ. CWD có thể là một chuỗi, byte hoặc đối tượng giống như đường dẫn. Trên POSIX, hàm tìm kiếm thực thi (hoặc cho mục đầu tiên trong ARGS) so với CWD nếu đường dẫn thực thi là một đường dẫn tương đối.path-like object. On POSIX, the function looks for executable (or for the first item in args) relative to cwd if the executable path is a relative path.

Thay đổi trong phiên bản 3.6: Tham số CWD chấp nhận một đối tượng giống như đường dẫn trên POSIX.cwd parameter accepts a path-like object on POSIX.

Thay đổi trong phiên bản 3.7: Tham số CWD chấp nhận một đối tượng giống như đường dẫn trên Windows.cwd parameter accepts a path-like object on Windows.

Thay đổi trong phiên bản 3.8: Tham số CWD chấp nhận đối tượng byte trên Windows.cwd parameter accepts a bytes object on Windows.

Nếu restore_signals là đúng (mặc định), tất cả các tín hiệu mà python đã đặt thành sig_ign được khôi phục thành sig_dfl trong quy trình con trước khi thực hiện. Hiện tại điều này bao gồm các tín hiệu Sigpipe, SigXFZ và SigXFSZ. (Chỉ Posix)

Đã thay đổi trong phiên bản 3.2: Restore_signals đã được thêm vào.restore_signals was added.

Nếu start_new_session là đúng thì cuộc gọi hệ thống

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
83 sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.

Tính khả dụng: Posix: POSIX

Đã thay đổi trong phiên bản 3.2: start_new_session đã được thêm vào.start_new_session was added.

Nếu Process_group là một số nguyên không âm, cuộc gọi hệ thống

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
84 sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.

Tính khả dụng: Posix: POSIX

Đã thay đổi trong phiên bản 3.2: start_new_session đã được thêm vào.process_group was added.

Nếu nhóm không phải là

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, cuộc gọi hệ thống setregid () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Nếu giá trị được cung cấp là một chuỗi, nó sẽ được tra cứu qua
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
86 và giá trị trong
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
87 sẽ được sử dụng. Nếu giá trị là một số nguyên, nó sẽ được truyền nguyên văn. (Chỉ Posix)

Tính khả dụng: Posix: POSIX

Mới trong phiên bản 3.9.

Nếu Extra_groups không phải là

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, cuộc gọi hệ thống setgroup () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Các chuỗi được cung cấp trong các nhóm thêm sẽ được tra cứu qua
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
86 và các giá trị trong
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
87 sẽ được sử dụng. Giá trị số nguyên sẽ được truyền nguyên văn. (Chỉ Posix)

Tính khả dụng: Posix: POSIX

Mới trong phiên bản 3.9.

Nếu Extra_groups không phải là

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, cuộc gọi hệ thống setgroup () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Các chuỗi được cung cấp trong các nhóm thêm sẽ được tra cứu qua
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
86 và các giá trị trong
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
87 sẽ được sử dụng. Giá trị số nguyên sẽ được truyền nguyên văn. (Chỉ Posix)

Tính khả dụng: Posix: POSIX

Mới trong phiên bản 3.9.

Nếu Extra_groups không phải là

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, cuộc gọi hệ thống setgroup () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Các chuỗi được cung cấp trong các nhóm thêm sẽ được tra cứu qua
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
86 và các giá trị trong
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
87 sẽ được sử dụng. Giá trị số nguyên sẽ được truyền nguyên văn. (Chỉ Posix)

Tính khả dụng: Posix: POSIX

Mới trong phiên bản 3.9.

Nếu Extra_groups không phải là

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, cuộc gọi hệ thống setgroup () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Các chuỗi được cung cấp trong các nhóm thêm sẽ được tra cứu qua
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
86 và các giá trị trong
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
87 sẽ được sử dụng. Giá trị số nguyên sẽ được truyền nguyên văn. (Chỉ Posix)

Nếu người dùng không phải là

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, cuộc gọi hệ thống setreuid () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Nếu giá trị được cung cấp là một chuỗi, nó sẽ được tra cứu qua
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
92 và giá trị trong
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
93 sẽ được sử dụng. Nếu giá trị là một số nguyên, nó sẽ được truyền nguyên văn. (Chỉ Posix)

Nếu UMASK không âm, cuộc gọi hệ thống Umask () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.must include a valid

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
95.

Nếu env không phải là

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, thì đó phải là một ánh xạ xác định các biến môi trường cho quy trình mới; Chúng được sử dụng thay vì hành vi mặc định của việc kế thừa môi trường quy trình hiện tại.Frequently Used Arguments. The universal_newlines argument is equivalent to text and is provided for backwards compatibility. By default, file objects are opened in binary mode.

Ghi chúencoding and errors were added.

Nếu được chỉ định, Env phải cung cấp bất kỳ biến nào cần thiết cho chương trình để thực thi. Trên Windows, để chạy tập hợp cạnh nhau, Env được chỉ định phải bao gồm

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
95 hợp lệ.text was added as a more readable alias for universal_newlines.

Nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản là đúng, các đối tượng tệp stdin, stdout và stderr được mở ở chế độ văn bản với mã hóa và lỗi được chỉ định, như được mô tả ở trên trong các đối số thường được sử dụng. Đối số Universal_Newlines tương đương với văn bản và được cung cấp cho khả năng tương thích ngược. Theo mặc định, các đối tượng tệp được mở ở chế độ nhị phân.

  • subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    
    98

  • subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    
    99

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    00

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    01

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    02

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    03

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    04

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    05

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    06

  • Mới trong phiên bản 3.6: Mã hóa và lỗi đã được thêm vào.

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    08

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    09

Mới trong phiên bản 3.7: Văn bản đã được thêm vào dưới dạng bí danh dễ đọc hơn cho Universal_Newlines.

Nếu được đưa ra, startupinfo sẽ là đối tượng

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
96, được truyền đến hàm
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
37 bên dưới. Creationflags, nếu được đưa ra, có thể là một hoặc nhiều cờ sau:The
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
11 parameter was added.

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
07

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())

Các đường ống có thể được sử dụng để thay đổi kích thước của đường ống khi

run(..., check=True, stdout=PIPE).stdout
8 được sử dụng cho stdin, stdout hoặc stderr. Kích thước của đường ống chỉ được thay đổi trên các nền tảng hỗ trợ điều này (chỉ Linux tại thời điểm viết này). Các nền tảng khác sẽ bỏ qua tham số này.auditing event
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
13 with arguments
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
14,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
15,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
16, and
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
17. The value for
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
15 may be a single string or a list of strings, depending on platform.

Mới trong phiên bản 3.10: Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
11 đã được thêm vào.Added context manager support.

Các đối tượng popen được hỗ trợ dưới dạng các trình quản lý ngữ cảnh thông qua câu lệnh

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
12: Khi thoát, các mô tả tệp tiêu chuẩn được đóng và quá trình được chờ đợi.Popen destructor now emits a
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
19 warning if the child process is still running.

Popen và các chức năng khác trong mô -đun này sử dụng nó nâng cao sự kiện kiểm toán

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
13 với các đối số
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
14,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
15,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
16 và
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
17. Giá trị cho
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
15 có thể là một chuỗi đơn hoặc danh sách các chuỗi, tùy thuộc vào nền tảng.Popen can use
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 in some cases for better performance. On Windows Subsystem for Linux and QEMU User Emulation, Popen constructor using
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 no longer raise an exception on errors like missing program, but the child process fails with a non-zero
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3.

Đã thay đổi trong phiên bản 3.2: Đã thêm hỗ trợ Trình quản lý bối cảnh.

Đã thay đổi trong phiên bản 3.6: Công cụ phá hủy popen hiện đang phát ra cảnh báo

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
19 nếu quá trình con vẫn đang chạy.

Đã thay đổi trong phiên bản 3.8: Popen có thể sử dụng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 trong một số trường hợp để có hiệu suất tốt hơn. Trên hệ thống con Windows cho mô phỏng người dùng Linux và QEMU, hàm tạo popen sử dụng
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 không còn gây ra ngoại lệ về các lỗi như bị thiếu chương trình, nhưng quá trình con không thành công với một
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3 khác không.

Ngoại lệ ha

Các trường hợp ngoại lệ được nêu ra trong quy trình trẻ em, trước khi chương trình mới bắt đầu thực thi, sẽ được nuôi dưỡng lại trong cha mẹ.

Ngoại lệ phổ biến nhất được nâng lên là

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
23. Điều này xảy ra, ví dụ, khi cố gắng thực thi một tệp không tồn tại. Các ứng dụng nên chuẩn bị cho các ngoại lệ
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
23. Lưu ý rằng, khi
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
46,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
23 sẽ được con nuôi chỉ khi không tìm thấy vỏ được chọn. Để xác định xem shell không tìm thấy ứng dụng được yêu cầu, cần phải kiểm tra mã trả về hoặc đầu ra từ quy trình con.

Các ngoại lệ được xác định trong mô -đun này tất cả kế thừa từ

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
0.

Cân nhắc về Bảo mật¶

Không giống như một số chức năng popen khác, việc triển khai này sẽ không bao giờ hoàn toàn gọi một shell hệ thống. Điều này có nghĩa là tất cả các nhân vật, bao gồm cả các metacharacters shell, có thể được truyền một cách an toàn cho các quá trình trẻ em. Nếu vỏ được gọi rõ ràng, thông qua

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
46, thì trách nhiệm của ứng dụng là đảm bảo rằng tất cả các chất trắng và metacharacters được trích dẫn một cách thích hợp để tránh các lỗ hổng tiêm vỏ. Trên một số nền tảng, có thể sử dụng
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
37 cho việc trốn thoát này.some platforms, it is possible to use
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
37 for this escaping.

Đối tượng Popen

Các thể hiện của lớp

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 có các phương pháp sau:

Popen.poll () ¶poll()

Kiểm tra nếu quá trình trẻ em đã chấm dứt. Đặt và trả về thuộc tính

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3. Nếu không, trả về
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1.

Popen.wait (thời gian chờ = không) ¶wait(timeout=None)

Chờ quá trình trẻ em chấm dứt. Đặt và trả về thuộc tính

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3.

Nếu quá trình không chấm dứt sau thời gian chờ giây, hãy tăng ngoại lệ

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
5. Nó là an toàn để bắt được ngoại lệ này và thử lại sự chờ đợi.

Ghi chú

Điều này sẽ bế tắc khi sử dụng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1 và quy trình con tạo ra đủ đầu ra cho một đường ống sao cho nó chặn chờ bộ đệm ống OS chấp nhận thêm dữ liệu. Sử dụng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
4 khi sử dụng đường ống để tránh điều đó.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

Popen.Commenticate (input = none, timeout = none) ¶communicate(input=None, timeout=None)

Tương tác với quy trình: Gửi dữ liệu đến Stdin. Đọc dữ liệu từ stdout và stderr, cho đến khi đạt được phần cuối. Đợi quá trình chấm dứt và đặt thuộc tính

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3. Đối số đầu vào tùy chọn phải là dữ liệu được gửi đến quy trình con hoặc
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, nếu không có dữ liệu nào được gửi cho trẻ. Nếu các luồng được mở ở chế độ văn bản, đầu vào phải là một chuỗi. Nếu không, nó phải là byte.

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
48 Trả về một tuple
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
49. Dữ liệu sẽ là chuỗi nếu các luồng được mở ở chế độ văn bản; Nếu không, byte.

Lưu ý rằng nếu bạn muốn gửi dữ liệu đến quy trình STDIN, bạn cần tạo đối tượng popen bằng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8. Tương tự, để có được bất cứ thứ gì khác ngoài
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 trong kết quả, bạn cũng cần cung cấp cho
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 và/hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1.

Nếu quá trình không chấm dứt sau khi hết thời gian chờ, ngoại lệ

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
5 sẽ được nâng lên. Bắt được ngoại lệ này và giao tiếp thử lại sẽ không mất bất kỳ đầu ra nào.

Quá trình trẻ em không bị giết nếu hết thời gian chờ, vì vậy để dọn dẹp đúng một ứng dụng được cư xử tốt nên tiêu diệt quy trình trẻ em và kết thúc giao tiếp:

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()

Ghi chú

Điều này sẽ bế tắc khi sử dụng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1 và quy trình con tạo ra đủ đầu ra cho một đường ống sao cho nó chặn chờ bộ đệm ống OS chấp nhận thêm dữ liệu. Sử dụng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
4 khi sử dụng đường ống để tránh điều đó.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

Popen.Commenticate (input = none, timeout = none) ¶send_signal(signal)

Tương tác với quy trình: Gửi dữ liệu đến Stdin. Đọc dữ liệu từ stdout và stderr, cho đến khi đạt được phần cuối. Đợi quá trình chấm dứt và đặt thuộc tính

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3. Đối số đầu vào tùy chọn phải là dữ liệu được gửi đến quy trình con hoặc
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, nếu không có dữ liệu nào được gửi cho trẻ. Nếu các luồng được mở ở chế độ văn bản, đầu vào phải là một chuỗi. Nếu không, nó phải là byte.

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
48 Trả về một tuple
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
49. Dữ liệu sẽ là chuỗi nếu các luồng được mở ở chế độ văn bản; Nếu không, byte.

Ghi chú

Điều này sẽ bế tắc khi sử dụng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1 và quy trình con tạo ra đủ đầu ra cho một đường ống sao cho nó chặn chờ bộ đệm ống OS chấp nhận thêm dữ liệu. Sử dụng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
4 khi sử dụng đường ống để tránh điều đó.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.terminate()

Popen.Commenticate (input = none, timeout = none) ¶

Tương tác với quy trình: Gửi dữ liệu đến Stdin. Đọc dữ liệu từ stdout và stderr, cho đến khi đạt được phần cuối. Đợi quá trình chấm dứt và đặt thuộc tính
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3. Đối số đầu vào tùy chọn phải là dữ liệu được gửi đến quy trình con hoặc
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, nếu không có dữ liệu nào được gửi cho trẻ. Nếu các luồng được mở ở chế độ văn bản, đầu vào phải là một chuỗi. Nếu không, nó phải là byte.
kill()

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
48 Trả về một tuple
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
49. Dữ liệu sẽ là chuỗi nếu các luồng được mở ở chế độ văn bản; Nếu không, byte.

Lưu ý rằng nếu bạn muốn gửi dữ liệu đến quy trình STDIN, bạn cần tạo đối tượng popen bằng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8. Tương tự, để có được bất cứ thứ gì khác ngoài
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 trong kết quả, bạn cũng cần cung cấp cho
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 và/hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1.

Nếu quá trình không chấm dứt sau khi hết thời gian chờ, ngoại lệ
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
5 sẽ được nâng lên. Bắt được ngoại lệ này và giao tiếp thử lại sẽ không mất bất kỳ đầu ra nào.
args

Quá trình trẻ em không bị giết nếu hết thời gian chờ, vì vậy để dọn dẹp đúng một ứng dụng được cư xử tốt nên tiêu diệt quy trình trẻ em và kết thúc giao tiếp:

Dữ liệu đọc được đệm trong bộ nhớ, vì vậy không sử dụng phương pháp này nếu kích thước dữ liệu lớn hoặc không giới hạn.

Popen.send_signal (tín hiệu) ¶stdin

Gửi tín hiệu tín hiệu cho trẻ.

Không làm gì nếu quá trình hoàn thành.stdout

Trên Windows, Sigterm là bí danh cho

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
55. CTRL_C_EVENT và CTRL_BREAK_EVENT có thể được gửi đến các quy trình bắt đầu với tham số CreationFlags bao gồm
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
99.

Popen.erminate () ¶stderr

Ngừng đứa trẻ. Trên Posix OSS, phương pháp gửi Sigterm cho đứa trẻ. Trên Windows, hàm API Win32

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
57 được gọi để ngăn chặn đứa trẻ.

Popen.pid¶pid

ID quy trình của quá trình trẻ em.

Lưu ý rằng nếu bạn đặt đối số shell thành

run(..., check=True, stdout=PIPE).stdout
7, đây là ID quy trình của vỏ sinh sản.

Popen.returncode¶returncode

Mã trả về con, được đặt bởi

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
77 và
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
78 (và gián tiếp bởi
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
48). Giá trị
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 chỉ ra rằng quá trình này đã chấm dứt.

Giá trị âm

Popen(['/bin/sh', '-c', args[0], args[1], ...])
4 chỉ ra rằng đứa trẻ bị chấm dứt bởi tín hiệu
Popen(['/bin/sh', '-c', args[0], args[1], ...])
5 (chỉ POSIX).

Windows Popen Helpers¶

Lớp

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
96 và các hằng số sau chỉ có sẵn trên Windows.

classSubProcess.StartUpInfo (*, dwflags = 0, hstdinput = none, hstDoutput = none, hstderror = none, wshowWindow = 0, lpattributelist = none)subprocess.STARTUPINFO(*, dwFlags=0, hStdInput=None, hStdOutput=None, hStdError=None, wShowWindow=0, lpAttributeList=None)

Hỗ trợ một phần của cấu trúc Windows StartupInfo được sử dụng để tạo

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6. Các thuộc tính sau đây có thể được đặt bằng cách truyền chúng dưới dạng đối số chỉ từ khóa.

Thay đổi trong phiên bản 3.7: Hỗ trợ đối số chỉ từ khóa đã được thêm vào.Keyword-only argument support was added.

dwflags¶

Một trường bit xác định xem một số thuộc tính

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
96 có được sử dụng khi quá trình tạo ra một cửa sổ.

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW

hstdinput¶

Nếu

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
86 chỉ định
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
87, thuộc tính này là tay cầm đầu vào tiêu chuẩn cho quy trình. Nếu
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
87 không được chỉ định, mặc định cho đầu vào tiêu chuẩn là bộ đệm bàn phím.

Hstdoutput¶

Nếu

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
86 chỉ định
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
87, thuộc tính này là tay cầm đầu ra tiêu chuẩn cho quy trình. Mặt khác, thuộc tính này bị bỏ qua và mặc định cho đầu ra tiêu chuẩn là bộ đệm cửa sổ console.

hstderror¶

Nếu

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
86 chỉ định
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
87, thuộc tính này là xử lý lỗi tiêu chuẩn cho quy trình. Mặt khác, thuộc tính này bị bỏ qua và mặc định cho lỗi tiêu chuẩn là bộ đệm cửa sổ console.

wshowwindow¶

Nếu

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
86 chỉ định
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
94, thuộc tính này có thể là bất kỳ giá trị nào có thể được chỉ định trong tham số
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
95 cho hàm showWindow, ngoại trừ
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
96. Nếu không, thuộc tính này bị bỏ qua.

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
97 được cung cấp cho thuộc tính này. Nó được sử dụng khi
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 được gọi với
subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
46.

lpattributelist¶

Một từ điển các thuộc tính bổ sung để tạo quy trình như được đưa ra trong

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
00, xem UpdateProcThreadAttribution.

Các thuộc tính được hỗ trợ:

handle_list

Trình tự của tay cầm sẽ được kế thừa. Close_fds phải đúng nếu không trống.

Các tay cầm phải được thực hiện tạm thời bằng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
01 khi được chuyển cho hàm tạo
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6, nếu không
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
23 sẽ được nâng lên với lỗi Windows
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
04 (87).

Cảnh báo

Trong một quy trình đa luồng, sử dụng thận trọng để tránh các tay cầm bị rò rỉ có thể di truyền khi kết hợp tính năng này với các cuộc gọi đồng thời đến các chức năng tạo quy trình khác kế thừa tất cả các tay cầm như

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
05. Điều này cũng áp dụng cho chuyển hướng xử lý tiêu chuẩn, tạm thời tạo ra tay cầm có thể thừa kế.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng số

Mô -đun

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
0 hiển thị các hằng số sau.

SubProcess.std_input_handle¶STD_INPUT_HANDLE

Thiết bị đầu vào tiêu chuẩn. Ban đầu, đây là bộ đệm đầu vào bảng điều khiển,

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
07.

Subprocess.std_output_handle¶STD_OUTPUT_HANDLE

Thiết bị đầu ra tiêu chuẩn. Ban đầu, đây là bộ đệm màn hình bảng điều khiển hoạt động,

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
08.

SubProcess.std_error_handle¶STD_ERROR_HANDLE

Thiết bị lỗi tiêu chuẩn. Ban đầu, đây là bộ đệm màn hình bảng điều khiển hoạt động,

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
08.

quá trình phụ.sw_hide¶SW_HIDE

Giấu cửa sổ. Một cửa sổ khác sẽ được kích hoạt.

phụSTARTF_USESTDHANDLES

Chỉ định rằng các thuộc tính

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
10,
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
11 và
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
12 chứa thông tin bổ sung.

SubProcess.startf_useshowwindow¶STARTF_USESHOWWINDOW

Chỉ định rằng thuộc tính

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
13 chứa thông tin bổ sung.

SubProcess.Create_New_Console¶CREATE_NEW_CONSOLE

Quá trình mới có một giao diện điều khiển mới, thay vì kế thừa bảng điều khiển cha mẹ của nó (mặc định).

SubProcess.Create_New_Process_group¶CREATE_NEW_PROCESS_GROUP

Tham số

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 để chỉ định rằng một nhóm quy trình mới sẽ được tạo. Cờ này là cần thiết để sử dụng
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
16 trên quy trình con.

Cờ này bị bỏ qua nếu

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
98 được chỉ định.

phụABOVE_NORMAL_PRIORITY_CLASS

Tham số

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên trên trung bình.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng sốBELOW_NORMAL_PRIORITY_CLASS

Mô -đun

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
0 hiển thị các hằng số sau.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng sốHIGH_PRIORITY_CLASS

Mô -đun

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
0 hiển thị các hằng số sau.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng sốIDLE_PRIORITY_CLASS

Mô -đun

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
0 hiển thị các hằng số sau.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng sốNORMAL_PRIORITY_CLASS

Mô -đun

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
0 hiển thị các hằng số sau.

Mới trong phiên bản 3.7.

phụREALTIME_PRIORITY_CLASS

Tham số

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên thời gian thực. Bạn hầu như không bao giờ nên sử dụng realtime_priority_class, bởi vì điều này làm gián đoạn các luồng hệ thống quản lý đầu vào chuột, đầu vào bàn phím và xả đĩa nền. Lớp này có thể phù hợp với các ứng dụng mà nói chuyện trực tiếp với phần cứng hoặc thực hiện các tác vụ ngắn gọn nên bị gián đoạn hạn chế.

Mới trong phiên bản 3.7.

SubProcess.Create_NO_Window¶CREATE_NO_WINDOW

Tham số

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 để chỉ định rằng một quy trình mới sẽ không tạo cửa sổ.

Mới trong phiên bản 3.7.

quá trình phụ .detached_process¶DETACHED_PROCESS

Tham số

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 để chỉ định rằng một quy trình mới sẽ không kế thừa bảng điều khiển cha mẹ của nó. Giá trị này không thể được sử dụng với create_new_console.

Mới trong phiên bản 3.7.

SubProcess.Create_Default_error_Mode¶CREATE_DEFAULT_ERROR_MODE

Tham số

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 để chỉ định rằng một quy trình mới không kế thừa chế độ lỗi của quá trình gọi. Thay vào đó, quy trình mới có chế độ lỗi mặc định. Tính năng này đặc biệt hữu ích cho các ứng dụng shell đa luồng chạy với các lỗi cứng bị vô hiệu hóa.

Mới trong phiên bản 3.7.

phụCREATE_BREAKAWAY_FROM_JOB

Tham số

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 để chỉ định rằng một quy trình mới không liên quan đến công việc.

Mới trong phiên bản 3.7.

API cấp cao cũ hơn

Trước Python 3.5, ba chức năng này bao gồm API cấp cao để xử lý phụ. Bây giờ bạn có thể sử dụng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 trong nhiều trường hợp, nhưng rất nhiều mã hiện tại gọi các chức năng này.

Subprocess.call (args, *, stdin = none, stdout = none, stderr = none, shell = falsecall(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

Chạy lệnh được mô tả bởi Args. Đợi lệnh hoàn thành, sau đó trả lại thuộc tính

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3.

Mã cần bắt giữ Stdout hoặc Stderr nên sử dụng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 thay thế:

Để ngăn chặn stdout hoặc stderr, cung cấp giá trị

run(..., check=True, stdout=PIPE).stdout
9.

Các đối số hiển thị ở trên chỉ là một số thông thường. Chữ ký chức năng đầy đủ giống như hàm xây dựng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 - hàm này chuyển tất cả các đối số được cung cấp khác ngoài thời gian chờ trực tiếp đến giao diện đó.

Ghi chú

Không sử dụng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1 với chức năng này. Quá trình con sẽ chặn nếu nó tạo ra đủ đầu ra cho một đường ống để lấp đầy bộ đệm ống OS vì các đường ống không được đọc từ đó.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

Subprocess.check_call (args, *, stdin = none, stdout = none, stderr = none, shell = falsecheck_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

Chạy lệnh với các đối số. Đợi lệnh hoàn thành. Nếu mã trả về bằng không thì hãy quay lại, nếu không thì tăng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9. Đối tượng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 sẽ có mã trả về trong thuộc tính
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3. Nếu
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
6 không thể bắt đầu quá trình, nó sẽ tuyên truyền ngoại lệ đã được nêu ra.

Mã cần bắt giữ Stdout hoặc Stderr nên sử dụng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 thay thế:

Để ngăn chặn stdout hoặc stderr, cung cấp giá trị

run(..., check=True, stdout=PIPE).stdout
9.

Các đối số hiển thị ở trên chỉ là một số thông thường. Chữ ký chức năng đầy đủ giống như hàm xây dựng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 - hàm này chuyển tất cả các đối số được cung cấp khác ngoài thời gian chờ trực tiếp đến giao diện đó.

Ghi chú

Không sử dụng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1 với chức năng này. Quá trình con sẽ chặn nếu nó tạo ra đủ đầu ra cho một đường ống để lấp đầy bộ đệm ống OS vì các đường ống không được đọc từ đó.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

Subprocess.check_call (args, *, stdin = none, stdout = none, stderr = none, shell = falsecheck_output(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, errors=None, universal_newlines=None, timeout=None, text=None, **other_popen_kwargs)

Chạy lệnh với các đối số. Đợi lệnh hoàn thành. Nếu mã trả về bằng không thì hãy quay lại, nếu không thì tăng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9. Đối tượng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 sẽ có mã trả về trong thuộc tính
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3. Nếu
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
6 không thể bắt đầu quá trình, nó sẽ tuyên truyền ngoại lệ đã được nêu ra.

Subprocess.check_output (args, *, stdin = none, stderr = none, shell = false, cwd = none, encoding = none

Chạy lệnh với các đối số và trả về đầu ra của nó.

run(..., check=True, stdout=PIPE).stdout

Nếu mã trả về không khác, nó sẽ tăng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9. Đối tượng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 sẽ có mã trả về trong thuộc tính
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3 và bất kỳ đầu ra nào trong thuộc tính
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
57.

Điều này tương đương với:

Các đối số hiển thị ở trên chỉ là một số thông thường. Chữ ký chức năng đầy đủ phần lớn giống như

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 - hầu hết các đối số được chuyển trực tiếp qua giao diện đó. Tồn tại một độ lệch API so với hành vi
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5: vượt qua
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
60 sẽ hoạt động giống như
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
61 (hoặc
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
62, tùy thuộc vào các đối số khác) thay vì sử dụng xử lý tệp đầu vào tiêu chuẩn của cha mẹ.Frequently Used Arguments and
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5.

Để nắm bắt lỗi tiêu chuẩn trong kết quả, hãy sử dụng

Popen(['/bin/sh', '-c', args[0], args[1], ...])
8:

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
0

Mới trong phiên bản 3.1.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

Thay đổi trong phiên bản 3.4: Hỗ trợ cho đối số từ khóa đầu vào đã được thêm vào.Support for the input keyword argument was added.

Thay đổi trong phiên bản 3.6: Mã hóa và lỗi đã được thêm vào. Xem

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 để biết chi tiết.encoding and errors were added. See
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 for details.

Mới trong phiên bản 3.7: Văn bản đã được thêm vào dưới dạng bí danh dễ đọc hơn cho Universal_Newlines.text was added as a more readable alias for universal_newlines.

Thay thế các chức năng cũ bằng mô -đun Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."]) 0

Trong phần này, A A trở thành B Bi có nghĩa là B có thể được sử dụng thay thế cho a.

Ghi chú

Tất cả các chức năng của một người khác trong phần này đều không thành công (ít nhiều) nếu không thể tìm thấy chương trình thực hiện; Thay vào đó, các thay thế của B Biêu tăng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
23 thay thế.

Ngoài ra, việc thay thế sử dụng

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
2 sẽ thất bại với
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 nếu thao tác được yêu cầu tạo mã trả lại khác không. Đầu ra vẫn có sẵn dưới dạng thuộc tính
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
57 của ngoại lệ được nâng lên.

Trong các ví dụ sau, chúng tôi giả định rằng các chức năng liên quan đã được nhập từ mô -đun

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
0.

Thay thế /Bin /SH Shell Lệnh thay thế SHELL/bin/sh shell command substitution¶

becomes:

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
1

Thay thế đường ống shell

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
2

becomes:

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
3

Cuộc gọi

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
73 Sau khi bắt đầu P2 rất quan trọng để P1 nhận được sigpipe nếu P2 thoát trước P1.

Ngoài ra, đối với đầu vào đáng tin cậy, hỗ trợ đường ống riêng Shell Shell vẫn có thể được sử dụng trực tiếp:

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
2

becomes:

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
5

Thay thế ________ 305¶

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
6

Notes:

  • Gọi chương trình thông qua vỏ thường không được yêu cầu.

  • Giá trị trả về

    >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    32 được mã hóa khác với giá trị
    Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    05.

  • Hàm

    Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    05 bỏ qua các tín hiệu sigint và sigquit trong khi lệnh đang chạy, nhưng người gọi phải làm điều này một cách riêng biệt khi sử dụng mô -đun
    Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    0.

Một ví dụ thực tế hơn sẽ trông như thế này:

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
7

Thay thế cho gia đình ____379

Ví dụ P_Nowait:

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
8

Ví dụ P_Wait:

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
9

Ví dụ về vector:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
0

Ví dụ về môi trường:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
1

Thay thế Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."]) 80, Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."]) 81, ________ 382¶

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
2

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
3

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
4

Xử lý mã trả lại dịch như sau:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
5

Thay thế các chức năng từ mô -đun Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."]) 83

Ghi chú

Tất cả các chức năng của một người khác trong phần này đều không thành công (ít nhiều) nếu không thể tìm thấy chương trình thực hiện; Thay vào đó, các thay thế của B Biêu tăng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
23 thay thế.

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7

Ngoài ra, việc thay thế sử dụng

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
2 sẽ thất bại với
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 nếu thao tác được yêu cầu tạo mã trả lại khác không. Đầu ra vẫn có sẵn dưới dạng thuộc tính
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
57 của ngoại lệ được nâng lên.

  • Trong các ví dụ sau, chúng tôi giả định rằng các chức năng liên quan đã được nhập từ mô -đun

    Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    0.

  • Thay thế /Bin /SH Shell Lệnh thay thế SHELL

  • Thay thế đường ống shell

  • Cuộc gọi

    Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    73 Sau khi bắt đầu P2 rất quan trọng để P1 nhận được sigpipe nếu P2 thoát trước P1.

Ngoài ra, đối với đầu vào đáng tin cậy, hỗ trợ đường ống riêng Shell Shell vẫn có thể được sử dụng trực tiếp:

Thay thế ________ 305¶

Gọi chương trình thông qua vỏ thường không được yêu cầu.getstatusoutput(cmd, *, encoding=None, errors=None)

Giá trị trả về

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
32 được mã hóa khác với giá trị
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
05.

Hàm

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
05 bỏ qua các tín hiệu sigint và sigquit trong khi lệnh đang chạy, nhưng người gọi phải làm điều này một cách riêng biệt khi sử dụng mô -đun
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
0.Frequently Used Arguments for more details.

Một ví dụ thực tế hơn sẽ trông như thế này:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
8

Thay thế cho gia đình ____379: Unix, Windows.

Ví dụ P_Nowait:Windows support was added.

Ví dụ P_Wait:

Ví dụ về vector:Added encoding and errors arguments.

Ví dụ về môi trường:getoutput(cmd, *, encoding=None, errors=None)

Thay thế

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
80,
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
81, ________ 382¶

Xử lý mã trả lại dịch như sau:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
9

Thay thế cho gia đình ____379: Unix, Windows.

Ví dụ P_Nowait:Windows support added

Ví dụ về vector:Added encoding and errors arguments.

Ví dụ về môi trường:

Thay thế Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."]) 80, Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."]) 81, ________ 382¶

Xử lý mã trả lại dịch như sau:

  1. Thay thế các chức năng từ mô -đun

    Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    83

  2. Nếu đối số CMD cho các hàm popen2 là một chuỗi, thì lệnh được thực thi thông qua /bin /sh. Nếu đó là một danh sách, lệnh được thực thi trực tiếp.

  3. Một dấu ngoặc kép có trước một dấu gạch chéo ngược được hiểu là một dấu ngoặc kép theo nghĩa đen.

  4. Backslashes được giải thích theo nghĩa đen, trừ khi chúng ngay lập tức đi trước dấu ngoặc kép.

  5. Nếu dấu gạch chéo ngược ngay trước dấu ngoặc kép, mỗi cặp dấu gạch chéo ngược được hiểu là một dấu gạch chéo ngược theo nghĩa đen. Nếu số lượng dấu gạch chéo ngược là số lẻ, dấu gạch chéo ngược cuối cùng sẽ thoát khỏi dấu ngoặc kép tiếp theo như được mô tả trong Quy tắc 3.

Xem thêm

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
98

Mô -đun cung cấp chức năng cho các dòng lệnh phân tích và thoát.

Vô hiệu hóa việc sử dụng Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."]) 99 hoặc ________ 400¶

Trên Linux,

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
0 mặc định sử dụng hệ thống
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
99 gọi nội bộ khi an toàn để làm như vậy thay vì
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
03. Điều này cải thiện đáng kể hiệu suất.

Nếu bạn từng gặp phải một tình huống được cho là rất bất thường khi bạn cần ngăn chặn

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
99 được Python sử dụng, bạn có thể đặt thuộc tính
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
05 thành giá trị sai.

SubProcess._USE_VFORK = FALSE # Xem vấn đề Cpython Gh-Nnnnnn.

Cài đặt Điều này không có tác động đến việc sử dụng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
00 có thể sử dụng
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
99 trong nội bộ trong quá trình triển khai LIBC của nó. Có một thuộc tính
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
08 tương tự nếu bạn cần ngăn chặn việc sử dụng điều đó.

SubProcess._USE_POSIX_SPAWN = FALSE # Xem Cpython vấn đề GH-Nnnnnn.

Có thể đặt những thứ này thành sai trên bất kỳ phiên bản Python nào. Chúng sẽ không có tác dụng đối với các phiên bản cũ hơn khi không được hỗ trợ. Không cho rằng các thuộc tính có sẵn để đọc. Mặc dù tên của họ, một giá trị thực sự không chỉ ra rằng hàm tương ứng sẽ được sử dụng, chỉ có điều đó có thể.

Vui lòng giải quyết các vấn đề bất cứ khi nào bạn phải sử dụng các nút riêng này với cách tái tạo vấn đề bạn đang thấy. Liên kết đến vấn đề đó từ một bình luận trong mã của bạn.

Mới trong phiên bản 3.8:

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
09
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
09

Mới trong phiên bản 3.11:

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
10
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
10