Python 如何创建在解析IP时扫描web服务器的脚本';s进入列表(py3)
对于我的简介类,我必须创建一个扫描web服务器的脚本。扫描应该在模块中实现,用户输入和输出应该在导入模块的脚本中实现 到目前为止,我的实际脚本(不是模块我有这个) 我现在有这个模块Python 如何创建在解析IP时扫描web服务器的脚本';s进入列表(py3),python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,对于我的简介类,我必须创建一个扫描web服务器的脚本。扫描应该在模块中实现,用户输入和输出应该在导入模块的脚本中实现 到目前为止,我的实际脚本(不是模块我有这个) 我现在有这个模块 #!/usr/bin/python3 import socket s=socket.socket() s.connect((hostServer, 80)) # I'm getting an error for using hostServer but
#!/usr/bin/python3
import socket
s=socket.socket()
s.connect((hostServer, 80)) # I'm getting an error for using hostServer but
how else should I pass the list of IP
addresses into my module?
s.send(b'GET / HTTP/1.0\n\n')
s.recv(1024)
while True:
data.s.recv(1024)
if data:
print('Recieved {} bytes from client at {}.\n{}'.format(len(data), addr, data))
conn.sendall(data)
break
s.close()
有人能指导我走出这片混乱吗如何收听多个端口
您需要为每个端口创建一个套接字,这意味着您不能仅通过导入模块来使用普通方法调用
将代码提取到函数或类中。以下是一个例子:
然后为每个端口创建一个侦听器实例
from socketmodule import PortListener
listener = PortListener('127.0.0.1', 5000)
可能的问题:
对于保留端口(1024以下的任何端口),您需要根访问权限来侦听它
您可能需要考虑使用循环来创建侦听器池,以从多个端口接收数据。还请确保在完成与web服务器的交互后关闭套接字
#!/usr/bin/python3
import socket
class PortListener(object):
CHUNK_SIZE = 1024
def __init__(self, host_server, port):
self.host = host_server
self.port = port
def listen(self):
self.socket = socket.socket()
self.socket.connect((self.host, self.port))
self.socket.send(b'GET / HTTP/1.0\n\n')
while True:
data = self.socket.recv(self.CHUNK_SIZE)
if data:
print('Recieved {} bytes from client at {}.\n{}'.format(len(data), '{}:{}'.format(self.host,
self.port), data))
def close(self, type, value, traceback):
self.socket.close()
from socketmodule import PortListener
listener = PortListener('127.0.0.1', 5000)