[英]Execute time on HTML by Python
我正在嘗試用Python構建一個網站,但是當我執行這一行時:
from flask import Flask
import time
import socket
from subprocess import Popen,PIPE
from datetime import datetime
app = Flask(__name__)
host_name = {'HostName' : socket.gethostname()}
cmd = "ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'"
p = Popen(cmd, shell=True,stdout=PIPE, stderr=PIPE)
ip_address, err = p.communicate()
ip_address = {"IP" : ip_address[:-1]}
@app.route("/")
def info():
dateNow = {'Date' : str(time.strftime("%d/%m/%Y"))}
timeNow = {'Time' : str(time.strftime("%H:%M:%S"))}
return '''
<html>
<head>
<title>Galileo Test Page</title>
</head>
<body>
<h1 align = "center"> Welcome to VGU </h1>
<p align = "center">
Host Name: ''' + host_name['HostName'] + '''<br>
IP Address: ''' + ip_address['IP'] + '''<br>
Date: ''' + dateNow['Date'] + '''<br>
Current time:'''+timeNow['Time']+'''<br>
</p>
</body>
</html>
'''
該網站顯示錯誤
TypeError:只能將str(不是“bytes”)連接到str。
你能幫我解決這個問題嗎? 謝謝P / s:定義了所有變量。
p.communicate()
返回一個編碼字符串,因此您需要對其進行解碼。 time.strftime
也會返回字符串,因此無需將其轉換為字符串。
from flask import Flask
import time
import socket
from subprocess import Popen, PIPE
from datetime import datetime
app = Flask(__name__)
host_name = {'HostName': socket.gethostname()}
cmd = "ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'"
p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
ip_address, err = p.communicate()
ip_address, err = ip_address.decode(), err.decode()
ip_address = {"IP": ip_address[:-1]}
@app.route("/")
def info():
dateNow = {'Date': time.strftime("%d/%m/%Y")}
timeNow = {'Time': time.strftime("%H:%M:%S")}
return '''
<html>
<head>
<title>Galileo Test Page</title>
</head>
<body>
<h1 align = "center"> Welcome to VGU </h1>
<p align = "center">
Host Name: ''' + host_name['HostName'] + '''<br>
IP Address: ''' + ip_address['IP'] + '''<br>
Date: ''' + dateNow['Date'] + '''<br>
Current time:'''+timeNow['Time']+'''<br>
</p>
</body>
</html>
'''
app.run()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.