popen

executing commands via sockets with popen()

女生的网名这么多〃 提交于 2019-12-17 20:41:20
问题 Can anybody give me a hand trying to implement the following server and client?: The server: #include <stdlib.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> int main(void) { int sock = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in serv_addr = { 0 }; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(1234); bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); listen

subprocess Popen blocking PyQt GUI

旧城冷巷雨未停 提交于 2019-12-17 19:57:11
问题 I'm trying to build a simple gui for a video converter application called "HandBrake" using PyQt. My problem is that when I choose a video file to convert, subprocess Popen starts handbrake application with the necessary args but while waiting for handbrake to finish the gui gets blocked so I can't do any changes. (Ex: I can't disable the pushButton nor change its text) I'm not looking for a more complicated solution such as progressbar etc. but I'd like to simply disable the button and

kill a process started with popen

假如想象 提交于 2019-12-17 15:25:29
问题 After opening a pipe to a process with popen , is there a way to kill the process that has been started? (Using pclose is not what I want because that will wait for the process to finish, but I need to kill it.) 回答1: Don't use popen() , write your own wrapper that does what you'd like. It's fairly straightforward to fork() , and then replace stdin & stdout by using dup2() , and then calling exec() on your child. That way, your parent will have the exact child PID, and you can use kill() on

subprocess.Popen() IO redirect

橙三吉。 提交于 2019-12-17 12:03:49
问题 Trying to redirect a subprocess' output to a file. server.py: while 1: print "Count " + str(count) sys.stdout.flush() count = count + 1 time.sleep(1) Laucher: cmd = './server.py >temp.txt' args = shlex.split(cmd) server = subprocess.Popen( args ) The output appear on screen, temp.txt remains empty. What am I doing wrong? As background I am trying to capture the output of a program that has already been written. I cannot use: server = subprocess.Popen( [exe_name], stdin=subprocess.PIPE, stdout

Python模块-subprocess模块

陌路散爱 提交于 2019-12-16 17:17:25
Run()方法 >>> a = subprocess.run(['df','-h']) 文件系统 容量 已用 可用 已用% 挂载点 udev 468M 0 468M 0% /dev tmpfs 98M 7.4M 91M 8% /run /dev/sda1 39G 5.0G 32G 14% / tmpfs 488M 216K 488M 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 488M 0 488M 0% /sys/fs/cgroup tmpfs 98M 84K 98M 1% /run/user/1000 >>> a CompletedProcess(args=['df', '-h'], returncode=0) >>> a.returncode # 获取命令执行结果的状态码 0 >>> a.args # 获取命令参数列表 ['df', '-h'] 直接把命令按照列表传入 如果想要读取命令执行的结果和错误,需要通过管道 >>> a = subprocess.run(['df','-h'],stdout=subprocess.PIPE,stderr=subprocess.PIPE) >>> a.stdout # 如果命令执行成功就能读出执行结果 b'\xe6\x96\x87\xe4\xbb\xb6\xe7\xb3\xbb

subprocess以及常用的封装函数

北城余情 提交于 2019-12-16 10:34:56
从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system、os.spawn 、os.popen 、popen2. 、commands. 不但可以调用外部的命令作为子进程,而且可以连接到子进程的input/output/error管道,获取相关的返回信息 一、subprocess以及常用的封装函数 运行python的时候,我们都是在创建并运行一个进程。像Linux进程那样,一个进程可以fork一个子进程,并让这个子进程exec另外一个程序。在Python中,我们通过标准库中的subprocess包来fork一个子进程,并运行一个外部的程序。 subprocess包中定义有数个创建子进程的函数,这些函数分别以不同的方式创建子进程,所以我们可以根据需要来从中选取一个使用。另外subprocess还提供了一些管理标准流(standard stream)和管道(pipe)的工具,从而在进程间使用文本通信。 subprocess.call() 父进程等待子进程完成 返回退出信息(returncode,相当于Linux exit code) subprocess.check_call() 父进程等待子进程完成 返回0 检查退出信息,如果returncode不为0,则举出错误subprocess

python subprocess Popen

天大地大妈咪最大 提交于 2019-12-15 19:14:25
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> subprocess.Popen(args ,bufsize=0 ,executable=None ,stdin=None ,stdout=None ,stderr=None ,preexec_fn=None ,close_fds=False ,shell=False ,cwd=None ,env=None ,universal_newlines=False ,startupinfo=None ,creationflags=0) 参数args可以是字符串或者序列类型(如:list,元组),用于指定进程的可执行文件及其参数.如果是序列类型,第一个元素通常是可执行文件的路径.我们也可以显式的使用executeable参数来指定可执行文件的路径.在windows操作系统上,Popen通过调用 CreateProcess()来创建子进程,CreateProcess接收一个字符串参数,如果args是序列类型,系统将会通过list2cmdline()函数将序列类型转换为字符串。 参数bufsize:指定缓冲.我到现在还不清楚这个参数的具体含义,望各个大牛指点. 参数executable用于指定可执行程序.一般情况下我们通过args参数来设置所要运行的程序.如果将参数shell设为True

Python subprocess on Windows 7 64bit - no output when stdout=PIPE

ⅰ亾dé卋堺 提交于 2019-12-14 03:45:39
问题 Apologies for another question about Python subprocesses, but I couldn't find an answer to this one. I am having trouble with some Python code which calls a subprocess on Windows 7 64-bit. When the subprocess's stdout is sent to a pipe, no output is produced. The subprocess appears to run and terminate without problems, it just doesn't produce any output. EDIT: The same code works correctly on WinXP 32bit, so I updated the question title. # (listing 1) from subprocess import * #cmdline= (a

Python - Windows - Popen(shlex.split(command), shell=False causes OSError: [Errno 2] No such file or directory

两盒软妹~` 提交于 2019-12-13 22:01:32
问题 I am running this code which works fine in OSX but causes an error on Windows: command = "C:\\progra~2\\itms\\iTMSTransporter -m verify -f /Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp -u username -p password -o /Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp/LOGFILE.txt -s provider -v eXtreme" self.process1 = Popen(shlex.split(command), shell=False, stdin=PIPE) The error I am recieving on Windows is: WindowsError: [Error 2] The system cannot find the file specified Why is it giving me this error on

Spawning a non-child process in python

老子叫甜甜 提交于 2019-12-13 21:24:34
问题 I need to create spawn off a process in python that allows the calling process to exit while the child is still running. What is an effective way to do this? Note: I'm running on a UNIX environment. 回答1: Terminating the parent process does not terminate child processes in Unix-like operating systems, so you don't need to do anything special. Just start your subprocesses with subprocess.Popen and terminate the main process. The orphaned processes will automatically be adopted by init. 来源: