Python:转换输出并访问HTTP
我有一个子流程:Python:转换输出并访问HTTP,python,http,Python,Http,我有一个子流程: import subprocess subprocess.call(['/home/pi/script','-c' "echo '$OUTPUT'"]) 在$OUTPUT中,我将收到一个URL 如何将$OUTPUT转换为字符串(URL) 我如何访问URL(是一个只有一个数字1-5的网站)并将URL中的整数转换回整数变量 对于第一个问题,您可以使用以下内容: # Assume cmd = the process you want to execute with its para
import subprocess
subprocess.call(['/home/pi/script','-c' "echo '$OUTPUT'"])
在$OUTPUT
中,我将收到一个URL
$OUTPUT
转换为字符串(URL)对于第一个问题,您可以使用以下内容:
# Assume cmd = the process you want to execute with its parameters
# In your case, cmd = ['/home/pi/script','-c' "echo '$OUTPUT'"]
pipe = subprocess.Popen(cmd, stdout = subprocess.PIPE)
result = pipe.stdout.read()
#=> Result contains your URL, as a String
“1.如何将中的$OUTPUT转换为字符串(URL)?”
这就是“echo'$OUTPUT'
所做的。然后从子流程对象的标准管道读取它。(这意味着您需要使用subprocess.Popen()
)
“2.我如何访问URL(一个只有一个数字1-5的网站)并将URL中的整数转换回整数变量?”
使用
urllib
,或者在Python 3中使用http.request
subprocess.call的返回不是管道,也不是您以后使用的对象,它是整数。您考虑的是subprocess.Popen。但它仍然不是管道。import urllib giveTheInt=[urllib.urlopen(url)],但我只收到:IOError:[Errno socket error][Errno 110]连接超时。无线连接没问题,我已经连接到internet。Regebo与库urllib我有这个问题:导入urllib giveTheInt=[urllib.urlopen(url)],但我只收到:IOError:[Errno socket error][Errno 110]连接超时。无线网络没问题,我已经连接到互联网了。@user1865863:你所做的就是打开连接。您还需要从中读取(),以获得结果。对于进一步的问题,请开始一个新问题。