Pythonの関数内から変数の値を取得しようとしていますが、少し問題があります。
uri = None
clientHostname = None
clientIP = None
...
# Handles requests
class DataHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(dServe):
dServe.send_response(200)
dServe.send_header("connection-type", "text/html")
dServe.end_headers()
def do_GET(dServe):
global uri
dServe.send_response(200)
dServe.send_header("connection-type", "text/html")
uri = dServe.path
dServe.end_headers()
clientHostname = dServe.address_string()
clientIP = dServe.connection.getsockname()[0]
print(uri)
print(clientHostname)
print(clientIP)
これを実行しようとすると、値「None」のみが返されます。ポインタはありますか?これはPython(2.7)への私の最初の試みであり、私はまだPython(2.7)のベルカーブを登っています。
私のfullsスクリプトは現在次のとおりです。
import time
import BaseHTTPServer
import StringIO
import csv
import sqlite3 as sql
import SimpleHTTPServer
import SocketServer
import multiprocessing
import os
# Initial Variables
appsTable = "Apps"
groupsTable = "Groups"
hostName = 'localhost'
dataPort = 277
filePort = 438
dbFile = "Get-App.db"
uri = None
clientHostname = None
clientIP = None
if os.name == "nt":
dbPath = "Dependencies\sqlite3"
else:
dbPath = "Dependencies/sqlite3"
# Checks for DB and creates if not found
if os.path.exists(dbFile):
print("Database found!")
else:
print("Creating New Database...")
os.system("echo .tables | " + dbPath + " " + dbFile)
sqlConnect = None
try:
sqlConnect = sql.connect(dbFile)
sqlCursor = sqlConnect.cursor()
sqlCursor.execute('CREATE TABLE ' + appsTable + '(ID INTEGER PRIMARY KEY ASC, Name TEXT, InstallGroup TEXT, Version TEXT, Arch TEXT, Executable TEXT, Path TEXT, Command TEXT, Latest BOOL, Enabled BOOL)')
sqlCursor.execute('CREATE TABLE ' + groupsTable + '(ID INTEGER PRIMARY KEY ASC, Name TEXT, AppList TEXT, Version TEXT, Arch TEXT, Latest BOOL, Enabled BOOL)')
finally:
if sqlConnect:
sqlConnect.close
# Serves files
def file_serve():
if not os.path.exists(dbFile):
os.makedirs(appsTable)
os.chdir(appsTable)
FileHandler = SimpleHTTPServer.SimpleHTTPRequestHandler
fileServer = SocketServer.TCPServer(("", filePort), FileHandler)
fileServer.serve_forever()
# Handles requests
class DataHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(dServe):
dServe.send_response(200)
dServe.send_header("connection-type", "text/html")
dServe.end_headers()
def do_GET(dServe):
global uri
dServe.send_response(200)
dServe.send_header("connection-type", "text/html")
uri = dServe.path
dServe.end_headers()
clientHostname = dServe.address_string()
clientIP = dServe.connection.getsockname()[0]
dServe.wfile.write(uri)
print(uri)
print(clientHostname)
print(clientIP)
# This is what's written to the page:
uriPar = csv.reader(uri, delimiter='?')
uriEnt = csv.reader(uriPar[0], delimiter='/')
sqlConnect = sql.connect(dbFile)
for entity in uriEnt:
with sqlConnect:
sqlCursor = sqlConnect.cursor()
if entity[1] == "app":
if len(entity) == 3:
print clientHostname + " made request for app " + entity[2]
sqlCursor.execute("SELECT Executable,Path,Command FROM " + appsTable + " WHERE Latest='true' AND Enabled='true' AND Name='" + entity[2] + "'")
else:
print clientHostname + " made request for app " + entity[2] + ", specifically version " + entity[3]
sqlCursor.execute("SELECT Executable,Path,Command FROM " + appsTable + " WHERE Enabled='true' AND Version='" + entity[3] + "' AND Name='" + entity[2] + "'")
elif entity[1] == "group":
if len(entity) == 3:
print clientHostname + " made request for group " + entity[2]
sqlCursor.execute("SELECT AppList FROM " + groupsTable + " WHERE Latest='true' AND Enabled='true' AND Name='" + entity[2] + "'")
else:
print clientHostname + " made request for group " + entity[2] + ", specifically version " + entity[3]
sqlCursor.execute("SELECT AppList FROM " + groupsTable + " WHERE Enabled='true' AND Version='" + entity[3] + "' AND Name='" + entity[2] + "'")
rows = sqlCursor.fetchall()
for place in rows:
for position in place:
dServe.wfile.write(position)
dServe.wfile.write(",")
# Serves http
if __name__ == '__main__':
dataServer_class = BaseHTTPServer.HTTPServer
dataServer = dataServer_class((hostName, dataPort), DataHandler)
print time.asctime(), "Data Service Starts - %s:%s" % (hostName, dataPort)
print time.asctime(), "File Service Starts - %s:%s" % (hostName, filePort)
try:
fileServerThread = multiprocessing.Process(target=file_serve)
fileServerThread.start() # Starts File Server
dataServer.serve_forever() # Starts HTTP Server
except KeyboardInterrupt:
pass
dataServer.server_close()
fileServer.server_close()
print time.asctime(), "Server Stops - %s:%s" % (hostName, dataPort)