「これはヤバかったな」という遅刻エピソード

import sys
import socket
import getopt
import threading
import subprocess

# guro-baruhennsuunoteigi
listen = False
command = False
upload = False
execute = ""
target = ""
upload_destination = ""
port = 0

def usage():
print "BHP NET Tool"

def main():
global listen
global port
global execute
global command
global upload_destination
global target

if not len(sys.argv[1:]):
usage()

try:
opts, args = getopt.getopt(
sys.argv[1:],
"hle:t:p:cu:",
["help","listen","execute=","target=",
"port=","command","upload="])

except geopt.GetoptError as err:
print str(err)
usage()

for o,a in opts:
if o in ("-h","--help"):
usage()
elif o in ("-l","--listen"):
listen = True
elif o in ("-e","--execute"):
execute = a
elif o in ("-c","--commandshell"):
command = True
elif o in ("-u","--upload"):
upload_destination = a
elif o in ("-t","--target"):
target = a
elif o in ("-p","--port"):
port = int(a)
else:
assert False,"Unhandled Option"

#client mode
if not listen and len(target) and port > 0:
#put input into buffer from command line
buffer = sys.stdin.read()

#transmit date
client_sender(buffer)

if listen:
server_loop()

main()

def server_loop():
global target
global port

if not len(target):
target = "0.0.0.0"

server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

server.bind((target,port))

server.listen(5)

while True:
client_socket, addr = server.accept()

client_thread = threading.Thread(
target=client_handler, arg=(client_socket,))
client_tnread.start()

def run_command(command):
#delete \n
command = command.rstrip()

try:
output = subprocess.check_output(
command,stderr=subprocess.STDOUT,shell=True)
except:
output = "Failed to execute command.\r\n"

return output

def client_handler(client_socket):
global upload
global execute
global command

#upload fail
if len(upload_destination):

file_buffer = ""

while True:
data = client_socket.recv(1024)

if len(data) == 0:
break
else:
file_buffer += data

try:
file_descriptor = open(upload_destination,"wb")
file_descriptor.write(file_buffer)
file_descriptor.close()

client_socket.send(
"Successfully saved file to %s\r\n" % upload_destination)
except:
client_socket.send(
"Failed to save file to %s\r\n" % upload_destination)

#execute command
if len(execute):

output = run_command(execute)

client_socket.send(output)

#execute command shell
if command:

#prompt
prompt = "<BHP:#>"
client_socket.send(prompt)

while True:
cmd_buffer = ""
while "\n" not in cmd_buffer:
cmd_buffer += client_socket.recv(1024)

response = run_command(cmd_buffer)
response += prompt

client_socket.send(response)

上記のプログラムを、python bhnet.py -l -p 9999 -c と入力して走らせると、
File "bhnet2.py", line 82, in <module>
main()
File "bhnet2.py", line 80, in main
server_loop()
NameError: global name 'server_loop' is not defined
と表示されます。なんで?

A 回答 (2件)

○言語とかOSとかは書くようにしましょう。


○Pythonは空白がとても重要ですが、ここの書き込みはまとめられたり削除されたりします。
そのため、Pythonには不向きです。

という前置きをしておいて。

python 定義 順番

で検索、出てきたものを順番に読んでみましょう。

82行のmain()を実行した時点で、server_loop()が 定義されていない(not defined)だからです。
    • good
    • 0
この回答へのお礼

なるほど

ありがとうございます

お礼日時:2015/11/24 22:01

板違いです。



そのままです。
server_loopなんてどこにも宣言されていないからです。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!