python 端口扫描小程序 求助
求技术牛帮忙分析下代码,不知道哪里出错了。运行程序没有报错,但是输入IP 跟 port 之后没反应 程序就结束掉了。 求j指出问题给出意见。谢谢!{:7_112:}
-------------------------------------------------------------------------------------------------------------------------------------------
#-*-coding:utf-8-*-
import socket
from socket import *
tgthost = input("IP: ")
tgtport = input("Port: ")
def connscan(tgthost,tgtport):
try:
connskt = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
connskt.connect((tgthost,tgthost))
connskt.send('ViolentPython\r\n')
connskt.recv(1024)
print('[+]%d/tcp open' % tgtport)
print('[+] ' + str(tgthost))
except:
print('[-]%d/tcp closed' % tgtport)
finally:
connskt.close()
def portscan(tgthost,tgtport):
try:
tgtIP = gethostbyname(tgthost)
except:
print("[-] Cannot resolve ' %s ':Unknown host" %tgthost)
return
try:
tgtname = gethostbyaddr(tgtIP)
print('\n[+] Scann Results for: ' + tgtname)
except:
print('\n Scan Results for: ' + tgtIP)
tgtport 变量打错了吧 本帖最后由 1223253411 于 2020-6-15 10:46 编辑
首先你要知道,函数需要调用才会执行!
我相信你知道客户端的套接字需要连接上服务端的套接字
看看注释吧,因为涉及到了我的知识盲区,所以抱歉没法给你完整的代码,只能把你的代码中看上去错误的点指一下{:10_277:}
#-*-coding:utf-8-*-
import socket
from socket import *
tgthost = input("IP: ")
tgtport = input("Port: ")
def connscan(tgthost,tgtport):
try:
connskt = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
connskt.connect((tgthost,tgthost))# 端口错了吧???
connskt.send('ViolentPython\r\n') # 发的得是二进制,加个encode
connskt.recv(1024)# 这里的返回值有没有用呢
print('[+]%d/tcp open' % tgtport)
print('[+] ' + str(tgthost))
except:
print('[-]%d/tcp closed' % tgtport)
finally:
connskt.close()
def portscan(tgthost,tgtport): # 这个tgtport是干啥的,完全没用到啊
try:
tgtIP = gethostbyname(tgthost)
except:
print("[-] Cannot resolve ' %s ':Unknown host" %tgthost)
return
try:
tgtname = gethostbyaddr(tgtIP)
print('\n[+] Scann Results for: ' + tgtname)
except:
print('\n Scan Results for: ' + tgtIP)
随便说一下哦,其实主要的功能都在服务端那边,客户端就只负责请求发送与接收反馈而已,所以如果是逻辑上的问题应该都在服务端上 import socket
# from socket import *
# 多余的import 会覆盖前面的, socket.socket 覆盖socket
tgthost = input("IP: ")
tgtport = int(input("Port: "))
# input得到的都是str
def connscan(tgthost, tgtport):
try:
connskt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connskt.connect((tgthost, tgtport))# tgtport 变量
connskt.send(b'ViolentPython\r\n')
recv = connskt.recv(1024)
if recv:
print('[+]%d/tcp open' % tgtport)
print('[+] ' + str(tgthost))
connskt.close()
except ConnectionRefusedError:# 尽量少用* ,不利于排除
# raise
print('[-]%d/tcp closed/refused' % tgtport)
# finally: #finally close需要弄清楚是否所有except分支之后connskt都还在存活
def portscan(tgthost, tgtport):
try:
tgtIP = gethostbyname(tgthost)
except:
print("[-] Cannot resolve ' %s ':Unknown host" % tgthost)
return
try:
tgtname = gethostbyaddr(tgtIP)
print('\n[+] Scann Results for: ' + tgtname)
except:
print('\n Scan Results for: ' + tgtIP)
connscan(tgthost, tgtport)# 函数需调用
页:
[1]