简体   繁体   English

在Python Web中执行代码

[英]Executing Code in Python Web

im creating a webpage that will show me the SSID's available in my Network 即时通讯创建的网页将向我显示网络中可用的SSID

For this I have use this code: 为此,我使用以下代码:

nm-tool | nm-tool | grep "Infra" | grep“ Infra” | cut -d " " -f5 > /home/nunukene/SSID3.txt 剪切-d“” -f5> /home/nunukene/SSID3.txt

Im saving this into a file called SSID3, to later open it using the open() , read() and str.split 我将其保存到名为SSID3的文件中,以便稍后使用open(),read()和str.split打开它

My problem is that the code I want to execute in the page, wont get executed, the file SSID3.txt wont be created 我的问题是我要在页面中执行的代码不会被执行,不会创建文件SSID3.txt

This is my website code so far: 到目前为止,这是我的网站代码:

#!/usr/bin/python
import os
import subprocess
import cgitb

cgitb.enable()

a=os.system("""nm-tool | grep "Infra"  | cut -d " " -f5 > /home/nunukene/SSID3.txt""")

#SSIDStr = subprocess.check_output('nm-tool | grep "Infra" | cut -d " " -f5-6', shell=True)
#SSIDArray = str.split(SSIDStr)


ID = subprocess.check_output('ls', shell=True)
a='devilman'
print "Content-type:text/html\r\n\r\n"

print "<!DOCTYPE html>"
print "<html>"
print "<title> Not Hacking lol</title>"
print "<body>"
print "<h1> Join %s One of this networks <h1>" %(a)
print "</body>"
print "</html>" 

I dont know how to get this process working before the rest! 我不知道如何在剩下的时间里使这个过程正常进行!

  • I highly suggest using the logging module to help you diagnose where your problem is. 我强烈建议使用日志记录模块来帮助您诊断问题出在哪里。
  • a reason your subprocess call didn't work is you would need to make a list out of all of the arguments to the command. 您的子流程调用无效的原因是您需要从命令的所有参数中列出一个清单。

    SSIDStr = subprocess.check_output(['nm-tool','|','grep','"Infra"','|','cut','-d','" "','-f5']) SSIDStr = subprocess.check_output(['nm-tool','|','grep','“ Infra”','|','cut','-d','“”,'-f5'] )

    (I'm not sure if you have to escape the double-quotes in this string) (我不确定您是否必须转义此字符串中的双引号)

  • using the subprocess call this way avoids using the text file and it's permission problems that the web server user may be experiencing writing files. 通过这种方式使用子进程调用可以避免使用文本文件,并且Web服务器用户可能正在写入文件是权限问题。
  • you were re-writing the "a" variable and you weren't using the text file in the output. 您正在重新编写“ a”变量,并且未在输出中使用文本文件。
  • and I hope the cgitb.enable() line wasn't the problem. 我希望cgitb.enable()行不是问题。 Haven't seen that before. 没见过。 Have you thought of using Flask? 您是否考虑过使用Flask?
  • If you are using python 3 then the print statements need to be functions. 如果您使用的是python 3,则print语句需要是函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM