套接字编程知识回顾01
#-*- coding:utf8 -*- ''' Created on 2018年9月17日 @author: Administrator ''' import socket def main(target,port): sk=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sk.connect((target,port)) sk.send("successful to connection...".encode("utf-8")) response=sk.recv(1024) print(str(response,"utf-8")) if __name__=="__main__": target="127.0.0.1" port=8000 main(target,port)
# -*- coding:utf-8 -*- ''' Created on 2018��9��17�� @author: Administrator ''' import socket def main(target,port): server=socket.socket(socket.AF_INET,socket.SOCK_STREAM) server.bind((target,port)) server.listen(10) print("[*]启动监听%s:%d"%(target,port)) client,addr=server.accept() print("[*]接受来自 %s:%d"%(addr[0],addr[1])) response=client.recv(1024) print(response.decode("utf-8")) client.send("success to connection...".encode("utf8")) client.close() if __name__=="__main__": target="0.0.0.0" port=8000 main(target,port)
基于TCP的三次握手,对服务器的端口进行扫描。如果扫描成功,打印,否则不打印。
# -*- coding:utf-8 -*- ''' Created on 2018年9月18日 @author: Administrator ''' #1\定义函数,用来进行TCP端口扫描 #2、启动多线程运行扫描函数 import socket def portScan(target): client=socket.socket() try: client.connect((target,80)) print("[*] %s:80 开放"%target) client.close() except: pass def main(): pass if __name__=="__main__": target=input("please input ip address...") portScan(target)
利用多线程,提升扫描端口的效率
# -*- coding:utf-8 -*- ''' Created on 2018年9月18日 @author: Administrator ''' #1\定义函数,用来进行TCP端口扫描 #2、启动多线程运行扫描函数 import socket,time from threading import Thread def portScan(target,port): client=socket.socket() try: client.connect((target,port)) print("[*] %s:%d 开放"%(target,port)) except: pass finally: client.close() def main(target): threads=[] print("开始扫描:%s"%target) for port in range(1,1024): t=Thread(target=portScan,args=(target,port)) threads.append(t) t.start() for i in threads: # i.start() t.join() if __name__=="__main__": target=input("please input ip address...") start=time.time() main(target) print("over spend times %s"%(time.time()-start))