언어&플랫폼/python 2013. 11. 22. 14:12

1.

#!/usr/bin/python

import os
import socket

if os.name != "nt":
import fcntl
import struct

def get_interface_ip(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24])

def get_lan_ip():
ip = socket.gethostbyname(socket.gethostname())
if ip.startswith("127.") and os.name != "nt":
interfaces = [
"eth0",
"eth1",
"wlan1",
"wifi0",
"ath0",
"ath1",
"ppp0",
]
for ifname in interfaces:
try:
ip = get_interface_ip(ifname)
break
except IOError:
pass
return ip

print get_lan_ip()



2. http://myip.dnsdynamic.org/ 에 접속하면 자신의 아이피만 text로 나온다.

import requests


def external_ip(self):

        req = requests.get('http://myip.dnsdynamic.org/', headers=self.headers)

        ip = req.text

        return ip



'언어&플랫폼 > python' 카테고리의 다른 글

[python] dictionary 에 dictionary 붙이기  (0) 2013.12.18
[python] 띄어쓰기 제거  (0) 2013.12.10
[python] json decode  (0) 2013.12.10
Tutorial  (0) 2013.12.10
os.popen3와 subprocess.Popen  (0) 2013.12.09
posted by cozyboy
: