- Usando a biblioteca netifaces fica fácil pegar o ip da máquina
Pegando o endereço ip no python
1 >>> import netifaces
2 >>> netifaces.interfaces()
3 ['lo0', 'gif0', 'stf0', 'en0', 'en1', 'fw0']
4 >>> netifaces.ifaddresses('en0')
5 {18: [{'addr': '00:12:34:56:78:9a'}], 2: [{'broadcast':
6 '10.255.255.255', 'netmask': '255.0.0.0', 'addr': '10.16.1.4'}],
7 30: [{'netmask': 'ffff:ffff:ffff:ffff::', 'addr':
8 'fe80::123:4567:89ab:cdef%en0'}]}
9
10 # outro exemplo
11 python
12
13 s.getsockname()
14 Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
15 [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
16 Type "help", "copyright", "credits" or "license" for more information.
17 >>> import netifaces
18 >>> netifaces.ifaddresses('eth0')[2][0]['addr']
19 '192.168.0.31'
20 >>>
21
22 # usando a biblioteca socket
23 import socket
24
25 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
26
27 s.connect(("www.google.com",80))
28
29 # agora o ip externo
30 import urllib2
31 pub_ip = urllib2.urlopen("http://whatismyip.com/automation/n09230945.asp").read()
32 print pub_ip
Comments
Sign in to leave a comment.

