【发布时间】:2015-12-24 16:06:36
【问题描述】:
我在调用 Python 中设置为“全局”的变量时遇到问题。
我在脚本开头设置变量,然后在函数内部修改变量,然后在脚本结束时回显变量的值,以及我设置变量的值to in the function 似乎没有执行的功能。
脚本(简单的端口扫描程序,用于扫描 PC 的前 1000 个端口)。此外,我使用进程设置脚本的原因是因为我同时调用多个端口部分......但是我没有包含所有功能,因为它是一个相当长的脚本。
#!/usr/bin/env python
import socket
import subprocess
import sys
from datetime import datetime
from multiprocessing import Process
import random
import datetime
# Clear the screen
#subprocess.call('clear', shell=True)
# Ask for input
#remoteServer = raw_input("Enter a remote host to scan: ")
remoteServer = sys.argv[1]
remoteServerIP = socket.gethostbyname(remoteServer)
global openports
openports = []
def ports1():
global openports
for port in random.sample(range(1,1000), 999):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(.01)
result = sock.connect_ex((remoteServerIP, port))
if result == 0:
openports.append(format(port))
sock.close()
if __name__ == '__main__':
p1 = Process(target = ports1)
p1.start()
p1.join()
print openports
我知道变量正在设置,因为如果我从函数内部回显 openports 变量,它会返回正确的数据,但是当我在 name==main 部分中调用函数后回显它时,它作为一个空字符串返回。
【问题讨论】:
-
我在函数
ports1的for循环的第一行添加了print openports,它始终是[]- 我用你的参数google.com调用了你的脚本。跨度>
标签: python function multiprocessing port-scanning