zoukankan      html  css  js  c++  java
  • python 之返回本机*IP

    server 端

    #!/usr/local/anaconda3/bin/python
    
    import socket
    import threading
    
    # ====================================================
    # Author: chang - EMail:changbo@hmg100.com
    # Last modified: 2017-06-20
    # Filename: sendoutip.py
    # Description: send u out ip ,base socket
    # blog:http://www.cnblogs.com/changbo
    # ====================================================
    
    port = 8899
    host = 'x.x.x.xxx'
    
    
    def sendOut():
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind((host, port))
        s.listen(5)
    
        while True:
            connection, address = s.accept()
            ip, _ = address
            ip = (str(ip)).encode('utf-8')
            while True:
                data = connection.recv(1024)
                if not data:
                    break
                connection.send(ip)
                # s.close()
            connection.close()
        s.close()
    
    if __name__ == '__main__':
        t = threading.Thread(target=sendOut)
        t.start()

    client 端

    #!/usr/local/anaconda3/bin/python
    
    import socket
    import struct
    
    # ====================================================
    # Author: chang - EMail:changbo@hmg100.com
    # Last modified: 2017-06-20
    # Filename: sendoutip.py
    # Description: send u out ip ,base socket
    # blog:http://www.cnblogs.com/changbo
    # ====================================================
    
    port = 8899
    host = 'x.x.x.x'
    
    
    def getOut():
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host, port))
        data = 'hi'
        s.send(str(data).encode('utf-8'))
        results = s.recv(1024)
    
        print(str(results, 'utf-8'))
        s.close()
    
    if __name__ == '__main__':
        getOut()

    END!

  • 相关阅读:
    PHP 处理接口保证数据安全性
    zeromq使用模式实验总结
    文件描述符设置
    配置openssh实现sftp远程文件上传
    系统信号(signal)与其他(定时器,退出清理等)
    Python Subprocess Popen 管道阻塞问题分析解决
    fastcgi协议之一:定义
    命名空间与自动加载机制
    PSR规范
    细说php的异常和错误处理机制
  • 原文地址:https://www.cnblogs.com/changbo/p/7127783.html
Copyright © 2011-2022 走看看