U
U
ubushan2016-02-16 18:34:59
Flask
ubushan, 2016-02-16 18:34:59

How to display subprocess in Flask?

Guys, tell me, I'm a beginner in Python and Flask, I'm just starting, how to display Subprocess in the browser correctly?
For example, I write this

from app import app
import subprocess

@app.route('/')
@app.route('/index')

def index():
    user = { 'nickname': 'MyName' }
    p = subprocess.Popen("ping yandex.ru", stdout=subprocess.PIPE,)
    out = p.communicate(str)
    return '''
<html>
  <head>
    <title>MyFirstFlaskPage</title>
  </head>
  <body>
    <h1>Hello, ''' + user['nickname'] + '''</h1>
    <h1>Hello, ''' + out + '''</h1>
  </body>
</html>
'''


Flask starts the server, but in the browser it shows the error

builtins.TypeError


TypeError : Can't convert 'tuple' object to str implicitl y

from app import app
import subprocess

@app.route('/')
@app.route('/index')

def index():
    #user = { 'nickname': 'Ivan' }
    p = subprocess.Popen("ping yandex.ru", stdout=subprocess.PIPE,)
    out = p.communicate(str)
    return out


output in browser in one line :
Pinging yandex.ru [5.255.255.55] with 32 bytes of data: Reply from 5.255.255.55: bytes=32 time=2ms TTL=53 Reply from 5.255.255.55: bytes=32 time=2ms TTL =53 Reply from 5.255.255.55: bytes=32 time=2ms TTL=53 Reply from 5.255.255.55: bytes=32 time=2ms TTL=53 Ping statistics for 5.255.255.55: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 2ms, Maximum = 2ms, Average =

2ms

1st question in the first example, what is wrong?
2nd question how to display the correct structured output?


Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EugenKozlov, 2016-02-18
@EugenKozlov


Hello, ''' + user['nickname'] + '''
Hello, ''' + str(out) + '''

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question