User Tools

Site Tools


burim:project:bulksms:save_file.py
root@pi3plus:/var/www/html/upload/cgi-bin# pwd
/var/www/html/upload/cgi-bin

python script

  • save_file.py
#!/usr/bin/env python
import cgi, os
import cgitb; cgitb.enable()
import subprocess

try: # Windows needs stdio set for binary mode.
    import msvcrt
    msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
    msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
    pass

form = cgi.FieldStorage()

# A nested FieldStorage instance holds the file
fileitem = form['file']

# Test if the file was uploaded
if fileitem.filename:

    # strip leading path from file name
    # to avoid directory traversal attacks
    fn = os.path.basename(fileitem.filename)
    open('files/' + fn, 'wb').write(fileitem.file.read())
    subprocess.call("./files/script_sendsms.sh", shell=True)
    message = 'The file "' + fn + '" was uploaded successfully'
#    subprocess.call("script_test.sh, shell=True)
else:
    message = 'No file was uploaded'

print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,)

New version reading input text

#!/usr/bin/env python
import cgi, os
import cgitb; cgitb.enable()
import subprocess

try: # Windows needs stdio set for binary mode.
    import msvcrt
    msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
    msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
    pass

#cgi
form = cgi.FieldStorage()

# A nested FieldStorage instance holds the file
fileitem = form['file']

testin = "Hello World"

#read textbox input
sms_message = form.getvalue('name')
fo = open("files/sms_message.txt", "wb")
fo.write(sms_message);
fo.close()

# Test if the file was uploaded
if fileitem.filename:

    # strip leading path from file name
    # to avoid directory traversal attacks
    fn = os.path.basename(fileitem.filename)
    open('files/' + fn, 'wb').write(fileitem.file.read())
    subprocess.call("./files/script_sendsms.sh", shell=True)
    message = 'The file "' + fn + '" was uploaded successfully'
#    subprocess.call("script_test.sh, shell=True)
else:
    message = 'No file was uploaded'

print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,)
burim/project/bulksms/save_file.py.txt · Last modified: 2018/09/04 21:46 by burim

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki