• 套接字编程知识回顾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)
    server
    # -*- 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)
    fuwuduan

     基于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))
  • 相关阅读:
    FTPUtil工具类
    maven配置默认jdk版本
    IDEA 使用lombok
    当前时间是否在区间范围内
    获取剩余的秒数
    java类执行顺序
    java8 关于Set集合的线程安全使用
    反射之设置对象字段值以及根据字段获取对象字段值
    ES方法使用注意
    java导出标题多行且合并单元格的EXCEL
  • 原文地址:https://www.cnblogs.com/Mengchangxin/p/9670859.html
Copyright © 2020-2023  润新知