ขอบคุณล่วงหน้าสำหรับความช่วยเหลือใด ๆ ฉันค่อนข้างใหม่กับ python และใหม่กว่ากับ html
เมื่อไม่กี่วันที่ผ่านมาฉันได้ลองสร้างเว็บเพจที่มีปุ่มเพื่อทำงานบนโฮมเซิร์ฟเวอร์
ในขณะนี้ ฉันมีสคริปต์หลามที่สร้างหน้าพร้อมปุ่ม:
(See the simplified example below. removed code to clean up post)
จากนั้นสคริปต์หลามซึ่งรันคำสั่งดังกล่าวและส่งออกไปที่ iframe
บนเพจ:
(See the simplified example below. removed code to clean up post)
สิ่งนี้จะส่งออกเอาต์พุตที่เสร็จสิ้นทั้งหมดหลังจากคำสั่งเสร็จสิ้น ฉันได้ลองเพิ่มตัวเลือก -u
ให้กับสคริปต์ python เพื่อเรียกใช้โดยไม่มีบัฟเฟอร์ ฉันได้ลองใช้ Python subprocess
ด้วยเช่นกัน ถ้ามันช่วยประเภทคำสั่งที่ฉันใช้อยู่คือ apt-get update
และสคริปต์ Python อื่น ๆ สำหรับการย้ายไฟล์และแก้ไขการอนุญาตของโฟลเดอร์
และเมื่อรันจากเทอร์มินัลเซิร์ฟเวอร์ Ubuntu ปกติ มันจะทำงานได้ดีและส่งออกแบบเรียลไทม์ และจากการวิจัยของฉัน มันควรจะส่งออกเมื่อมีการรันคำสั่ง
ใครช่วยบอกฉันทีว่าฉันไปไหนผิด? ฉันควรใช้ภาษาอื่นเพื่อทำหน้าที่นี้หรือไม่?
แก้ไขตัวอย่างแบบง่าย:
หน้าแรก:
#runcmd.html
<head>
<title>Admin Tasks</title>
</head>
<center>
<iframe src="/scripts/python/test/createbutton.py" width="650" height="800" frameborder="0" ALLOWTRANSPARENCY="true"></iframe>
<iframe width="650" height="800" frameborder="0" ALLOWTRANSPARENCY="true" name="display"></iframe>
</center>
สคริปต์ที่สร้างปุ่ม:
cmd_page = '<form action="/scripts/python/test/runcmd.py" method="post" target="display" >' + '<label for="run_update">run updates</label><br>' + '<input align="Left" type="submit" value="runupdate" name="update" title="run_update">' + "</form><br>" + "\n"
print ("Content-type: text/html")
print ''
print cmd_page
สคริปต์ที่ควรรันคำสั่ง:
# runcmd.py:
import os
import pexpect
import cgi
import cgitb
import sys
cgitb.enable()
fs = cgi.FieldStorage()
sc_command = fs.getvalue("update")
if sc_command == "runupdate":
cmd = "/usr/bin/sudo apt-get update"
pd = pexpect.spawn(cmd, timeout=None, logfile=sys.stdout)
print ("Content-type: text/html")
print ''
print "<pre>"
line = pd.readline()
while line:
line = pd.readline()
ฉันไม่ได้ทดสอบตัวอย่างที่เรียบง่ายข้างต้น ดังนั้นจึงไม่แน่ใจว่ามันใช้งานได้หรือไม่
แก้ไข:
ตัวอย่างแบบง่ายควรใช้งานได้ทันที
แก้ไข:
รหัส Imrans ด้านล่างหากฉันเปิดเบราว์เซอร์ไปที่ ip:8000 มันจะแสดงเอาต์พุตเหมือนกับที่ทำงานในเทอร์มินัลซึ่งเป็นสิ่งที่ฉันต้องการอย่างแน่นอน ยกเว้นว่าฉันใช้เซิร์ฟเวอร์ Apache สำหรับเว็บไซต์ของฉันและใช้ iframe เพื่อแสดงผลลัพธ์ ฉันจะทำอย่างไรกับ Apache?
แก้ไข:
ตอนนี้ฉันมีเอาต์พุตไปที่ iframe โดยใช้ตัวอย่าง Imrans ด้านล่าง แต่ดูเหมือนว่าจะบัฟเฟอร์เช่น:
If I have it (the script through the web server using curl ip:8000) run apt-get update in terminal it runs fine but when outputting to the web page it seems to buffer a couple of lines => output => buffer => ouput till the command is done.
But running other python scripts the same way buffer then output everything at once even with the -u flag. While again in terminal running curl ip:800 outputs like normal.
นั่นเป็นเพียงวิธีที่มันควรจะทำงาน?
แก้ไข 19-03-2014:
คำสั่ง bash / shell ใด ๆ ที่ฉันเรียกใช้โดยใช้วิธี Imrans ดูเหมือนว่าจะส่งออกไปยัง iframe ในเวลาใกล้เคียงเรียลไทม์ แต่ถ้าฉันรันสคริปต์ python ชนิดใดก็ตามผ่านสคริปต์นั้น เอาต์พุตจะถูกบัฟเฟอร์ จากนั้นจะส่งไปที่ iframe
ฉันอาจจำเป็นต้อง PIPE ผลลัพธ์ของสคริปต์ python ที่รันโดยสคริปต์ที่รันเว็บเซิร์ฟเวอร์หรือไม่
javascript
ในเบราว์เซอร์และกระบวนการย่อยบนเซิร์ฟเวอร์ เพื่อตอบคำถามของคุณ: stdout จากกระบวนการย่อยจะถูกส่งไปยังเบราว์เซอร์ - person jfs   schedule 15.03.2014-u
จึงไม่มีบัฟเฟอร์ แต่ละไบต์จะถูกส่งทันที) - person jfs   schedule 15.03.2014curl http://youhost.com/path/to/script.py
? - person jfs   schedule 17.03.2014HTTP/1.1
ไม่ใช่HTTP/1.0
- person jfs   schedule 17.03.2014HTTP/1.1 200 OK
(บรรทัดแรกในการตอบกลับจากเซิร์ฟเวอร์) - person jfs   schedule 17.03.2014curl -v http://youhost.com/path/to/script.py |& grep 'HTTP/'
คุณเห็นอะไร HTTP/1.0 หรือ HTTP/1.1 200 ตกลง - person jfs   schedule 26.04.2014python -u
แล้วหรือยัง ต่อไปนี้เป็นวิธีแก้ปัญหาหลายอย่างสำหรับปัญหาการบัฟเฟอร์บล็อกสำหรับกระบวนการย่อย 'stdout - person jfs   schedule 27.04.2014