subprocess

Day16 常用内置模块(一)

我与影子孤独终老i 提交于 2020-02-18 23:38:23
1.collections模块    Python有一些内置的数据类型,如:整型、浮点型、字符串、列表、元组、字典、集合、布尔值,除了这些之外,collections模块还提供了几个额外的数据类型:Counter、deque、defaultdict、namedtuple、OrderedDict等 (1)namedtuple具名元组 生成可以使用名字来访问元素内容的tuple,简单的来说就是生成有具体名字的元组 可以表示一个坐标、扑克牌等 1 # namedtuple具名元组 2 # 如想表示一个坐标p = (1,2) 3 point = namedtuple('坐标',['x','y']) 4 p = point(1,2) 5 print(p) 6 print(p.x) 7 # 表示一个三维坐标 8 point = namedtuple('坐标','x y z') # 也可以用字符串来表示,但是各字符之间要空格隔开 9 p = point(1,2,3) # 元素数量必须跟字符数量一致 10 print(p) 11 print(p.z) 12 # 表示扑克牌 13 card = namedtuple('扑克牌','花色 大小') 14 c = card('♤','2') 15 print(c) 16 print(c.花色) (2)deque双端队列 可以从左右两侧追加和推出对象

远程接发命令,黏包

落花浮王杯 提交于 2020-02-17 23:17:08
让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd) # import subprocess # ret = subprocess.Popen('dir',shell=True,stdout = subprocess.PIPE,stderr=subprocess.PIPE) # print(ret.stdout.read().decode('gbk')) # print(ret.stderr.read().decode('gbk')) 上面的结果的编码是以当前所在的系统为准的,如果是windows,那么res.stdout.read()读出的就是GBK编码的,在接收端需要用GBK解码 有且只能从管道里读一次结果 同时执行多条命令之后,得到的结果很可能只有一部分,在执行其他命令的时候又接收到之前执行的另外一部分结果,这种显现就是黏包。 注意:只有TCP有粘包现象,UDP永远不会粘包 基于tcp协议实现的远程执行命令 import socket # import subprocess # # sk = socket.socket() # sk.bind(('127.0.0.1',9000)) # sk.listen() # conn,addr = sk.accept() # # while True: # content = conn.recv

Using subprocess wait() and poll()

萝らか妹 提交于 2020-02-17 06:10:09
问题 I am trying to write a small app that uses the subprocess module. My program calls an external Bash command that takes some time to process. During this time, I would like to show the user a series of messages like this: Processing. Please wait... The output is foo() How can I do this using Popen.wait() or Popen.poll() . I have read that I need to use the Popen.returncode , but how I can get it to actively check the state, I don't know. 回答1: Both wait() (with timeout specified) and poll()

Unable to SSH via Python subprocess

情到浓时终转凉″ 提交于 2020-02-16 08:11:33
问题 I need to ssh into a machine via a bastion. Therefore the command is rather very long for this: ssh -i <pemfile location> -A -o 'proxycommand ssh -i <pemfile location> ec2-user@<bastion ip address> -W %h:%p' hadoop@<machine ip> This command is rather very long. So I tried to write a python script which takes ip addresses and pemfile location as inputs and does ssh. #!/usr/local/bin/python3 import argparse import subprocess import os import sys import errno parser = argparse.ArgumentParser

File not found error when launching a subprocess containing piped commands

会有一股神秘感。 提交于 2020-02-12 08:25:01
问题 I need to run the command date | grep -o -w '"+tz+"'' | wc -w using Python on my localhost. I am using subprocess module for the same and using the check_output method as I need to capture the output for the same. However it is throwing me an error : Traceback (most recent call last): File "test.py", line 47, in <module> check_timezone() File "test.py", line 40, in check_timezone count = subprocess.check_output(command) File "/usr/lib/python2.7/subprocess.py", line 537, in check_output

File not found error when launching a subprocess containing piped commands

此生再无相见时 提交于 2020-02-12 08:24:30
问题 I need to run the command date | grep -o -w '"+tz+"'' | wc -w using Python on my localhost. I am using subprocess module for the same and using the check_output method as I need to capture the output for the same. However it is throwing me an error : Traceback (most recent call last): File "test.py", line 47, in <module> check_timezone() File "test.py", line 40, in check_timezone count = subprocess.check_output(command) File "/usr/lib/python2.7/subprocess.py", line 537, in check_output

Curly Braces in python Popen

北城以北 提交于 2020-02-11 06:53:21
问题 Running subprocess won't handle curly braces correctly # Python 2.7.4 import subprocess subprocess.Popen('ls src/*.cpp',shell=True): src/tonemap.cpp src/pch.cpp subprocess.Popen('ls src/{t,p}*.cpp', shell=True) ls: cannot access src/{p,t}*.cpp: No such file or directory The same program will work on a different machine with python 2.7.2. Both systems use bash shells. Do you the reason and how can I fix it? EDIT: Invoking the command directly from the command line returns the correct result:

Curly Braces in python Popen

微笑、不失礼 提交于 2020-02-11 06:50:42
问题 Running subprocess won't handle curly braces correctly # Python 2.7.4 import subprocess subprocess.Popen('ls src/*.cpp',shell=True): src/tonemap.cpp src/pch.cpp subprocess.Popen('ls src/{t,p}*.cpp', shell=True) ls: cannot access src/{p,t}*.cpp: No such file or directory The same program will work on a different machine with python 2.7.2. Both systems use bash shells. Do you the reason and how can I fix it? EDIT: Invoking the command directly from the command line returns the correct result:

Curly Braces in python Popen

懵懂的女人 提交于 2020-02-11 06:50:05
问题 Running subprocess won't handle curly braces correctly # Python 2.7.4 import subprocess subprocess.Popen('ls src/*.cpp',shell=True): src/tonemap.cpp src/pch.cpp subprocess.Popen('ls src/{t,p}*.cpp', shell=True) ls: cannot access src/{p,t}*.cpp: No such file or directory The same program will work on a different machine with python 2.7.2. Both systems use bash shells. Do you the reason and how can I fix it? EDIT: Invoking the command directly from the command line returns the correct result:

Socket

泪湿孤枕 提交于 2020-02-10 05:37:50
学socket编程的意义   客户端/服务器架构(c/s架构) 硬件c/s架构(打印机) 软件c/s架构         互联网中处处是C/S架构     如某网站是服务端,你的浏览器是客户端(B/S架构也是C/S架构的一种)     腾讯作为服务端为你提供视频,你得下个腾讯视频客户端才能看它的视频   C/S架构与socket的关系:      socket就是为了更加便捷的完成C/S架构的开发 OSI七层    一个完整的计算机系统是由硬件、操作系统、应用软件三者组成,具备了这三个条件,一台计算机系统就可以自己跟自己玩了(打个单机游戏,玩个扫雷啥的)    如果你要跟别人一起玩,那你就需要上网了,什么是互联网?   互联网的核心就是由一堆协议组成,协议就是标准,比如全世界人通信的标准是英语。    人们按照分工不同把互联网协议从逻辑上划分了层级:     互联网协议按照功能不同分为osi七层或tcp/ip五层或tcp/ip四层:          每层常用的物理设备:         为何学习socket一定要先学习互联网协议:         1.首先:本节课程的目标就是教会你如何基于socket编程,来开发一款自己的C/S架构软件   2.其次:C/S架构的软件(软件属于应用层)是基于网络进行通信的   3.然后:网络的核心即一堆协议,协议即标准