简体   繁体   中英

Executing Code in Python Web

im creating a webpage that will show me the SSID's available in my Network

For this I have use this code:

nm-tool | grep "Infra" | cut -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

My problem is that the code I want to execute in the page, wont get executed, the file SSID3.txt wont be created

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'])

    (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.
  • you were re-writing the "a" variable and you weren't using the text file in the output.
  • and I hope the cgitb.enable() line wasn't the problem. Haven't seen that before. Have you thought of using Flask?
  • If you are using python 3 then the print statements need to be functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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