473,385 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Determine ip address

hi,
how can i use python to figure the ip address of the machine which
the python script is running on? I dont mean like 127.0.0.1....but i
want the external IP address (such as ipconfig on windows displays).

any ideas??

THanks

Jul 19 '05 #1
8 4788
On 15 Apr 2005 06:03:06 -0700, codecraig <co*******@gmail.com> wrote:
hi,
how can i use python to figure the ip address of the machine which
the python script is running on? I dont mean like 127.0.0.1....but i
want the external IP address (such as ipconfig on windows displays).


On Windows, this works:

socket.gethostbyname(socket.gethostname())

Is that OK on real operating systems too?

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 19 '05 #2
rbt
codecraig wrote:
hi,
how can i use python to figure the ip address of the machine which
the python script is running on? I dont mean like 127.0.0.1....but i
want the external IP address (such as ipconfig on windows displays).

any ideas??

THanks


To get the public IP (like when you're behind a wirless router, etc) you
may try doing something like this:

import urllib
import re
import time

ip_search = re.compile ('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')

try:
f = urllib.urlopen("http://www.ipchicken.com")
data = f.read()
f.close()
current_ip = ip_search.findall(data)
if current_ip:
print current_ip
time.sleep(3)
except Exception:
pass

HTH,

rbt
Jul 19 '05 #3
codecraig wrote:
hi,
how can i use python to figure the ip address of the machine which
the python script is running on? I dont mean like 127.0.0.1....but i
want the external IP address (such as ipconfig on windows displays).


I use the following (all on one line):

external_ip = os.popen("/sbin/ifconfig eth0|/bin/grep inet|/bin/awk
'{print $2}' | sed -e s/.*://", "r").read().strip()

Cheers,
Andy
Jul 19 '05 #4
In article <ma**************************************@python.o rg>,
Simon Brunning <si************@gmail.com> wrote:
On 15 Apr 2005 06:03:06 -0700, codecraig <co*******@gmail.com> wrote:
hi,
how can i use python to figure the ip address of the machine which
the python script is running on? I dont mean like 127.0.0.1....but i
want the external IP address (such as ipconfig on windows displays).


On Windows, this works:

socket.gethostbyname(socket.gethostname())

Is that OK on real operating systems too?


It will work sometimes, but there is nothing I know of
that specifically distinguishes "the" external network.
If you want something that reliably finds a network that
will be used for a certain type of connection, then the
best thing to do is make a connection like that, and
inspect the results. The getsockname() method shows the
IP address.

Donn Cave, do**@u.washington.edu
Jul 19 '05 #5
On 2005-04-15, codecraig <co*******@gmail.com> wrote:
hi,
how can i use python to figure the ip address of the machine which
the python script is running on? I dont mean like 127.0.0.1....but i
want the external IP address (such as ipconfig on windows displays).

any ideas??


I use this:

#conf.py
ifconfig = '/sbin/ifconfig'
iface = 'eth0'
telltale = 'inet addr:'
#addr.py
import commands

from conf import ifconfig, iface, telltale

def my_addr():
cmd = '%s %s' % (ifconfig, iface)
output = commands.getoutput(cmd)

inet = output.find(telltale)
if inet >= 0:
start = inet + len(telltale)
end = output.find(' ', start)
addr = output[start:end]
else:
addr = ''

Jul 19 '05 #6
import socket
print socket.gethostbyname( socket.gethostname() )

Jul 19 '05 #7
http://checkip.tk/

codecraig ha scritto:
hi,
how can i use python to figure the ip address of the machine which
the python script is running on? I dont mean like 127.0.0.1....but i
want the external IP address (such as ipconfig on windows displays).

any ideas??

THanks


Jul 19 '05 #8
[Format recovered from top-posting.]

on******@tenup.com writes:
codecraig ha scritto:
how can i use python to figure the ip address of the machine which
the python script is running on? I dont mean like 127.0.0.1....but i
want the external IP address (such as ipconfig on windows displays).


http://checkip.tk/


That won't work if you're on a NAT'ed network - it will instead return
the external address of the NAT gateway. ipconfig displays the ip
address(es) of the interfaces on the current machine.

You need to use the socket.socket.getsockname method:

py> import socket
py> s = socket.socket()
py> s.connect(("google.com", 80))
py> s.getsockname()
('192.168.1.1', 57581)
py>

The local ethernet card is 192.168.1.1, the local port for the socket
is 57581.

Note that the presence of multiple interface cards makes the question
ambiguous.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: siliconmike | last post by:
Hi, I'm looking for a reliable script that would connect to a host and somehow determine whether an email address is valid. since getmxrr() only gets the mx records.. Links/pointers ? Mike
7
by: none | last post by:
I want to determine the outside (non local, a.k.a. 127.0.0.x) ip addresses of my host. It seems that the socket module provides me with some nifty tools for that but I cannot get it to work...
17
by: Sue | last post by:
<html> Is there someone here that can help me validate the period as the fourth from the last character in an email address. There is other information and validation on the form I have to do but...
3
by: Demetri | last post by:
How do I find out what the network IP address is? For example if I have a cable modem with 68.57.200.50 as the IP address and the linksys router assigns PC's on the network 192.168.1.1 how do I...
4
by: Selden McCabe | last post by:
Does anyone know how to determine the client's MAC address from within ASP.Net? I suppose it would have to be java or vb script running on the browser, but I haven't been able to find an...
10
by: Raffi | last post by:
Hi, I'm looking for a way using JavaScript to determine if the browser window has an address bar. Thanks, Raffi
3
by: billie | last post by:
Hi all. I'm searching for a module that permits me to low-level interact with ethernet interfaces of my system. I would like to determine at least the first of the followings values: 1 - IP...
6
by: BA | last post by:
Hi Everyone, I have an application that sits behind a server farm, the application needs to pass its NLB IP address in the message that it sends to another service. From C# code, how can I...
1
by: =?Utf-8?B?QWdlbmR1bQ==?= | last post by:
I have an issue where I have a remote IP Address, and I need to discover the local network interface IP Address which is viewable to the remote IP Address (for UPnP document purposes). For...
2
by: Trmbne2000 | last post by:
Hi, I'm developing a service provider listing service. Part of the specification is to have both an address and a 'seasonal address' for each listing. The database stores the start and end months...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.