码迷,mamicode.com
首页 > 编程语言 > 详细

python 之返回本机外网IP

时间:2017-07-06 20:35:41      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:des   cep   print   address   threading   add   ==   star   code   

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!

python 之返回本机外网IP

标签:des   cep   print   address   threading   add   ==   star   code   

原文地址:http://www.cnblogs.com/changbo/p/7127783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!