IP address

2006-08-10 Thread Lad
I would like to record visitor's IP address.How can I do that in
Python?
Thanks for help
L

-- 
http://mail.python.org/mailman/listinfo/python-list


Ip address

2007-01-28 Thread Scripter47
How do i get my ip address?

in cmd.exe i just type "ipconfig" then it prints:
  ...
  IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
  ...
how can i do that in python??

-- 
http://mail.python.org/mailman/listinfo/python-list


IP Address Function

2009-07-07 Thread Fred Atkinson
Is there a Python function I can use to get the user's IP
address so I can display it on his browser?  

Regards, 




Fred 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address

2006-08-10 Thread Lad

Sybren Stuvel wrote:
> Lad enlightened us with:
> > I would like to record visitor's IP address.How can I do that in
> > Python?
>
> Too little information. Visitors of what?
>
> Sybren

I have a website written in Python and I would like to login every
visitor's IP address.
In other words, if a visitor come to my Python application, this
application will record his IP.
Is it possible?
Thank you
B.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address

2006-08-10 Thread Jon Ribbens
In article <[EMAIL PROTECTED]>, Lad wrote:
> I have a website written in Python and I would like to login every
> visitor's IP address.
> In other words, if a visitor come to my Python application, this
> application will record his IP.

Depending on what CGI framework you're using, something like:

  os.environ["REMOTE_ADDR"]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address

2006-08-10 Thread Lad

Sybren Stuvel wrote:
> Lad enlightened us with:
> > I have a website written in Python and I would like to login every
> > visitor's IP address.
>
> Ehm... that's probably "log" not "login".
>
> > In other words, if a visitor come to my Python application, this
> > application will record his IP.  Is it possible?
>
> Sure it is. At least, using most Python web-interfaces. I can't give
> you more info, since you don't describe anything about the website. Is
> it mod_python? Django? CGI? Something else? Try giving us some
> information we can work with!

It is Django with mod_python
Regards,
L

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address

2007-01-28 Thread Klaus Alexander Seistrup
Scripter47 wrote:

> How do i get my ip address?
>
> in cmd.exe i just type "ipconfig" then it prints:
>   ...
>   IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
>   ...
> how can i do that in python??

#v+

python -c 'import re, urllib; print re.findall("Your IP: (.+?)", 
urllib.urlopen("http://myip.dk/";).read())[0]'

#v-

Cheers,

-- 
Klaus Alexander Seistrup
http://klaus.seistrup.dk/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address

2007-01-28 Thread Klaus Alexander Seistrup
Scripter47 wrote:

>> python -c 'import re, urllib; print re.findall("Your IP: 
>> (.+?)", urllib.urlopen("http://myip.dk/";).read())[0]'
> 
> Hmm then you need Internet connecting.

That's what IP adresses are for...

> can i do it without that?

Perhaps you could use the method mentioned in
  http://mail.python.org/pipermail/python-list/1999-August/009153.html

> and it doesn't work either :(

Works for me:

#v+

[EMAIL PROTECTED]:~ $ python -c 'import re, urllib; print 
re.findall("Your IP: (.+?)", 
urllib.urlopen("http://myip.dk/";).read())[0]'
217.157.1.202
[EMAIL PROTECTED]:~ $

#v-

Cheers,

-- 
Klaus Alexander Seistrup
http://klaus.seistrup.dk/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ip address

2007-01-28 Thread Adam
Hey,

This will get your IP address:

###Code
print socket.gethostbyaddr(socket.gethostname())
('compname', [], ['192.168.1.2'])
End Code

If you are wanting to to communicate over the internet you will have 
to get the IP of you rounter. So you will have to either find a way to 
talk to your router or try and use an online service like these other 
guys suggest.

On Jan 28, 9:54 am, Scripter47 <[EMAIL PROTECTED]> wrote:
> How do i get my ip address?
>
> in cmd.exe i just type "ipconfig" then it prints:
>   ...
>   IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
>   ...
> how can i do that in python??

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address

2007-01-28 Thread Colin J. Williams
Klaus Alexander Seistrup wrote:
> Scripter47 wrote:
> 
>> How do i get my ip address?
>>
>> in cmd.exe i just type "ipconfig" then it prints:
>>   ...
>>   IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
>>   ...
>> how can i do that in python??
> 
> #v+
> 
> python -c 'import re, urllib; print re.findall("Your IP: 
> (.+?)", urllib.urlopen("http://myip.dk/";).read())[0]'
> 
> #v-
> 
> Cheers,
> 
Klaus,

Your one-liner doesn't work for me, with Windows XP, but the following 
does, within Python.

*** Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit 
(Intel)] on win32. ***
 >>> import re, urllib; print re.findall("Your IP: 
(.+?)", urllib.urlopen("http://myip.dk/";).read())[0]
69.157.68.189
 >>>

Colin W.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address

2007-01-28 Thread Klaus Alexander Seistrup
Adam wrote:

> This will get your IP address:
>
> ###Code
> print socket.gethostbyaddr(socket.gethostname())
> ('compname', [], ['192.168.1.2'])
> ####End Code

It will return an IP address, but not necessarily the one you want:

#v+

[EMAIL PROTECTED]:~ $ python -c 'import socket; print 
socket.gethostbyaddr(socket.gethostname())'
('zdani.szn.dk', [], ['2001:1448:89::1'])
[EMAIL PROTECTED]:~ $

#v-

Cheers,

-- 
Klaus Alexander Seistrup
http://klaus.seistrup.dk/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address

2007-01-28 Thread Klaus Alexander Seistrup
Colin J. Williams wrote:

> Your one-liner doesn't work for me, with Windows XP, but the 
> following does, within Python.

Could it be due to shell-escaping issues?  I don't know anything 
about Windows...

Cheers,

-- 
Klaus Alexander Seistrup
http://klaus.seistrup.dk/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ip address

2007-01-28 Thread Steve Holden
Adam wrote:
> Hey,
> 
> This will get your IP address:
> 
> ###Code
> print socket.gethostbyaddr(socket.gethostname())
> ('compname', [], ['192.168.1.2'])
> End Code
> 
> If you are wanting to to communicate over the internet you will have 
> to get the IP of you rounter. So you will have to either find a way to 
> talk to your router or try and use an online service like these other 
> guys suggest.

There is absolutely no need to know the IP address of "your router" to 
communicate with Internet devices. Either your IP layer is configured to 
know the addresses of one or more routers, or it has discovered those 
address by dynamic means, or you can't get off-net because there aren't 
any routers.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note:  http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ip address

2007-01-28 Thread Toby A Inkster
Steve Holden wrote:

> There is absolutely no need to know the IP address of "your router" to 
> communicate with Internet devices. Either your IP layer is configured to 
> know the addresses of one or more routers, or it has discovered those 
> address by dynamic means, or you can't get off-net because there aren't 
> any routers.

... or you can't get off-net because you don't *know* the routers.

-- 
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ip address

2007-01-28 Thread Toby A Inkster
Scripter47 wrote:

> How do i get my ip address?

Which IP address. One computer might have many IP addresses. (Indeed a
typical network-connected computer will tend to have at least one for each
connected network device, plus the special address 127.0.0.1 for the
loopback network.) How is Python supposed to know which IP address you
want?

If you don't care which address, the code supplied by Adam should work. If
you do care, then you'll probably need to write OS-specific code for each
platform you choose to support, probably parsing the output of ipconfig
(Windows) or ifconfig (Linux/UNIX/Mac) somehow.

Adam also says:
| If you are wanting to to communicate over the internet you will have 
| to get the IP of you rounter.

Not strictly true, but if your network uses NAT, and you want some host
outside your network to be able to *connect to you*, then yes, you need the
IP address of your router. If you're not using NAT, then you shouldn't
need to worry about your router, as IP addresses alone provide full
end-to-end routing. (Indeed that's the whole point of IP.)

-- 
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address

2007-01-28 Thread Gabriel Genellina

At Sunday 28/1/2007 10:28, Colin J. Williams wrote:


Klaus Alexander Seistrup wrote:
>
> python -c 'import re, urllib; print re.findall("Your IP: 
(.+?)", urllib.urlopen("http://myip.dk/";).read())[0]'

>

Your one-liner doesn't work for me, with Windows XP, but the following


On XP you should switch the usage of " and ' (double quotes are used 
to enclose command arguments with embedded spaces):


python -c "import re, urllib; print re.findall('Your IP: 
(.+?)', urllib.urlopen('http://myip.dk/').read())[0]"



--
Gabriel Genellina
Softlab SRL 







__ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: IP address

2007-01-28 Thread Garry Knight
Klaus Alexander Seistrup wrote:

> urllib.urlopen("http://myip.dk/";)

http://whatismyip.org gives it to you in a more usable format. But, as
others have pointed out, it might return your router's IP.

-- 
Garry Knight
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address

2007-01-28 Thread Beej
On Jan 28, 2:26 am, Klaus Alexander Seistrup <[EMAIL PROTECTED]> wrote:
> Scripter47 wrote:
> > How do i get my ip address?
>
> > in cmd.exe i just type "ipconfig" then it prints:
> >   ...
> >   IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
> >   ...
> > how can i do that in python??#v+
>
> python -c 'import re, urllib; print re.findall("Your IP: 
> (.+?)", urllib.urlopen("http://myip.dk/";).read())[0]'

This is extremely unlikely to return 192.168.1.10. :-)

(It will give you the address of your firewall or whatever is your 
gateway to the outside world...  which is a cool thing to know, but 
I'm not sure it's what the op's after.)

-Beej

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ip address

2007-01-28 Thread Adonis Vargas
Scripter47 wrote:
> How do i get my ip address?
> 
> in cmd.exe i just type "ipconfig" then it prints:
>  ...
>  IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
>  ...
> how can i do that in python??
> 

If you want to get your external IP you can do:

import urllib

checkIP = urllib.urlopen("http://checkip.dyndns.org";).read()
externalIP = checkIP.split()[-1].strip("")
print externalIP

Hope this helps.

Adonis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address

2007-01-29 Thread Scripter47
Beej skrev:
> On Jan 28, 2:26 am, Klaus Alexander Seistrup <[EMAIL PROTECTED]> wrote:
>> Scripter47 wrote:
>>> How do i get my ip address?
>>> in cmd.exe i just type "ipconfig" then it prints:
>>>   ...
>>>   IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
>>>   ...
>>> how can i do that in python??#v+
>> python -c 'import re, urllib; print re.findall("Your IP: 
>> (.+?)", urllib.urlopen("http://myip.dk/";).read())[0]'
> 
> This is extremely unlikely to return 192.168.1.10. :-)
> 
> (It will give you the address of your firewall or whatever is your 
> gateway to the outside world...  which is a cool thing to know, but 
> I'm not sure it's what the op's after.)
> 
> -Beej
> 

Exactly i wnat my local ip. sorry if i din't tell that ;)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ip address

2007-01-29 Thread Steve Holden
Toby A Inkster wrote:
> Steve Holden wrote:
> 
>> There is absolutely no need to know the IP address of "your router" to 
>> communicate with Internet devices. Either your IP layer is configured to 
>> know the addresses of one or more routers, or it has discovered those 
>> address by dynamic means, or you can't get off-net because there aren't 
>> any routers.
> 
> ... or you can't get off-net because you don't *know* the routers.
> 
What I know or don't know makes absolutely no difference to whether my 
computer can reach the Internet, it's a matter of whether the IP layes 
is configured to know the appropriate address to which it can hand off 
non-local traffic.

If you are trying to say that it's necessary to know the IP address of 
the routers in order to select a specific interface address from the 
available choices as "the Internet interface" then kindly say so and 
stop wallowing in semantic obscurity.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note:  http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007

-- 
http://mail.python.org/mailman/listinfo/python-list


Your IP Address

2005-07-31 Thread Your IP address


Your IP address


<!--
body {
background-color: ##BBE1DF;
}
.style1 {
font-size: 36px;
font-weight: bold;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
-->


http://www.ipimne.com";>Your IP 
address 
  
www.ipimne.com




-- 
http://mail.python.org/mailman/listinfo/python-list


Determine ip address

2005-04-15 Thread codecraig
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.1but i
want the external IP address (such as ipconfig on windows displays).

any ideas??

THanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP Address Function

2009-07-07 Thread Tim Harig
On 2009-07-08, Fred Atkinson  wrote:
>   Is there a Python function I can use to get the user's IP
> address so I can display it on his browser?  

If you are using CGI you can get it from the REMOTE_ADDR environmental
variable.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP Address Function

2009-07-07 Thread Chris Rebert
On Tue, Jul 7, 2009 at 6:45 PM, Fred Atkinson wrote:
>        Is there a Python function I can use to get the user's IP
> address so I can display it on his browser?

from socket import gethostname, gethostbyname
ip = gethostbyname(gethostname())

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP Address Function

2009-07-07 Thread Gabriel Genellina
En Tue, 07 Jul 2009 22:45:24 -0300, Fred Atkinson   
escribió:



Is there a Python function I can use to get the user's IP
address so I can display it on his browser?


There is a long distance between "Python" and "browser" - you'll have to  
tell us what is in between the two.
By example, do you have a server and the user connects to it? is it  
running Python? how do you run the Python application?
And why do you want to do that on the server side? Isn't easier to do that  
on the client side? What about proxies? NAT?


If using CGI, look at the REMOTE_ADDR environment variable.

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list


Re: IP Address Function

2009-07-08 Thread Fred Atkinson
On Tue, 07 Jul 2009 22:54:03 -0300, "Gabriel Genellina"
 wrote:

>En Tue, 07 Jul 2009 22:45:24 -0300, Fred Atkinson   
>escribió:
>
>>  Is there a Python function I can use to get the user's IP
>> address so I can display it on his browser?
>
>There is a long distance between "Python" and "browser" - you'll have to  
>tell us what is in between the two.

I want to have a Web page come up (written in Python, cgi)
that returns the IP address of the browser (user's PC's IP address).  

>By example, do you have a server and the user connects to it? is it  
>running Python? how do you run the Python application?
>And why do you want to do that on the server side? Isn't easier to do that  
>on the client side? What about proxies? NAT?

Yes.  By CGI.  

>If using CGI, look at the REMOTE_ADDR environment variable.

I did look at REMOTE_ADDR but I've been unable to figure out
the correct way to code it.  I've tried a number of ways but I've been
unsuccessful.  

Ideally, I'd like to store the brower's IP address in a string
and then print the string on the Web page from a Python CGI script.  

Regards, 



Fred 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP Address Function

2009-07-08 Thread Piet van Oostrum
>>>>> Fred Atkinson  (FA) wrote:

>FA> On Tue, 07 Jul 2009 22:54:03 -0300, "Gabriel Genellina"
>FA>  wrote:

>>> En Tue, 07 Jul 2009 22:45:24 -0300, Fred Atkinson   
>>> escribió:
>>> 
>>>> Is there a Python function I can use to get the user's IP
>>>> address so I can display it on his browser?
>>> 
>>> There is a long distance between "Python" and "browser" - you'll have to  
>>> tell us what is in between the two.

>FA>I want to have a Web page come up (written in Python, cgi)
>FA> that returns the IP address of the browser (user's PC's IP address).  

>>> By example, do you have a server and the user connects to it? is it  
>>> running Python? how do you run the Python application?
>>> And why do you want to do that on the server side? Isn't easier to do that  
>>> on the client side? What about proxies? NAT?

>FA>Yes.  By CGI.  

>>> If using CGI, look at the REMOTE_ADDR environment variable.

>FA>I did look at REMOTE_ADDR but I've been unable to figure out
>FA> the correct way to code it.  I've tried a number of ways but I've been
>FA> unsuccessful.  

>FA>Ideally, I'd like to store the brower's IP address in a string
>FA> and then print the string on the Web page from a Python CGI script.  

Something like:

#! /usr/bin/env python

import cgi
from os import getenv

print "Content-type: text/html"
print

ipaddr = (getenv("HTTP_CLIENT_IP") or
  getenv("HTTP_X_FORWARDED_FOR") or
  getenv("HTTP_X_FORWARDED_FOR") or
  getenv("REMOTE_ADDR") or
  "UNKNOWN")

print ipaddr


-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP Address Function

2009-07-08 Thread Fred Atkinson
On Wed, 08 Jul 2009 12:29:54 +0200, Piet van Oostrum 
wrote:

>Something like:
>
>#! /usr/bin/env python
>
>import cgi
>from os import getenv
>
>print "Content-type: text/html"
>print
>
>ipaddr = (getenv("HTTP_CLIENT_IP") or
>  getenv("HTTP_X_FORWARDED_FOR") or
>  getenv("HTTP_X_FORWARDED_FOR") or
>  getenv("REMOTE_ADDR") or
>  "UNKNOWN")
>
>print ipaddr

That did it.  

I wonder why they don't just have a function to return it instead of
putting you through all of that?  

At any rate, it works.  

Regards, 




  Fred 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP Address Function

2009-07-08 Thread Nobody
On Wed, 08 Jul 2009 20:53:12 -0700, Fred Atkinson wrote:

>>ipaddr = (getenv("HTTP_CLIENT_IP") or
>>  getenv("HTTP_X_FORWARDED_FOR") or
>>  getenv("HTTP_X_FORWARDED_FOR") or
>>  getenv("REMOTE_ADDR") or
>>  "UNKNOWN")
>>
>>print ipaddr
> 
> That did it.  
> 
> I wonder why they don't just have a function to return it instead of
> putting you through all of that?  

There's no unambiguous definition of "client IP", so you have to choose
which definition you want.

REMOTE_ADDR is set to the actual IP address from which the connection
originated (from getpeername()). If the connection was made via a proxy,
REMOTE_ADDR will contain the IP address of the proxy.

The others are set from HTTP headers. Proxies often add Client-IP
or X-Forwarded-For headers to indicate the originating IP address. OTOH,
there's no way to know if the headers are accurate; nuisance users may
add these headers to confuse poorly-conceived banning mechanisms.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP Address Function

2009-07-09 Thread Piet van Oostrum
> Fred Atkinson  (FA) wrote:

>FA> On Wed, 08 Jul 2009 12:29:54 +0200, Piet van Oostrum 
>FA> wrote:

>>> Something like:
>>> 
>>> #! /usr/bin/env python
>>> 
>>> import cgi
>>> from os import getenv
>>> 
>>> print "Content-type: text/html"
>>> print
>>> 
>>> ipaddr = (getenv("HTTP_CLIENT_IP") or
>>> getenv("HTTP_X_FORWARDED_FOR") or
>>> getenv("HTTP_X_FORWARDED_FOR") or
>>> getenv("REMOTE_ADDR") or
>>> "UNKNOWN")
>>> 
>>> print ipaddr

>FA> That did it.  

>FA> I wonder why they don't just have a function to return it instead of
>FA> putting you through all of that?  

I see now that I had   getenv("HTTP_X_FORWARDED_FOR") or
duplicated.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP Address Function

2009-07-09 Thread Tim Roberts
Fred Atkinson  wrote:
>
>I wonder why they don't just have a function to return it instead of
>putting you through all of that?  

In CGI, EVERYTHING gets communicated through environment variables.  That
(and the stdin stream) is really the only option, since you get a new
process for every request.

The Python CGI module doesn't provide a wrapper function because this
information is just not useful.  Most corporate users sit behind proxies,
so everyone at the company appears to come from the same IP.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Get Cliet IP Address

2009-08-02 Thread Fred Atkinson
What is the function to obtain the client browser's IP
address?   




Fred 
-- 
http://mail.python.org/mailman/listinfo/python-list


IP address of webserver

2007-01-27 Thread Johny
How can I find server's IP address?
>From console I can use ping, for example:

C:\RobotP\cgi-bin>ping www.google.com

Pinging www.google.com [209.85.129.147] with 32 bytes of data:

Reply from 209.85.129.147: bytes=32 time=30ms TTL=244
Reply from 209.85.129.147: bytes=32 time=30ms TTL=244
..
..
So I know  that www.google.com has 209.85.129.147  IP address.
But how can I find it directly from Python script?
Thanks for help
J.

-- 
http://mail.python.org/mailman/listinfo/python-list


Getting external IP address

2007-03-05 Thread Steven D'Aprano
I have a PC behind a firewall, and I'm trying to programmatically
determine the IP address visible from outside the firewall.

If I do this:

>>> import socket
>>> socket.gethostbyname(socket.gethostname())
'127.0.0.1'
>>> socket.gethostbyname_ex(socket.gethostname())
('localhost.localdomain', ['localhost'], ['127.0.0.1'])

I get my internal IP address, which is not what I want.

Other tricks, like parsing the output of os.system('/sbin/ifconfig eth0')
also give me my internal IP address.

I found this post on comp.lang.python a few years ago:

http://mail.python.org/pipermail/python-list/2003-March/192495.html

which seems like it _should_ do what I want, but it doesn't: I get an
exception.

>>> from httplib import HTTPConnection
>>> from xml.dom.ext.reader.Sax import FromXmlStream
>>> conn = HTTPConnection('xml.showmyip.com')
>>> conn.request('GET', '/')
>>> doc = FromXmlStream(conn.getresponse())
>>> print doc.getElementsByTagName('ip')[0].firstChild.data
Traceback (most recent call last):
  File "", line 1, in ?
  File "/usr/lib/python2.4/UserList.py", line 28, in __getitem__
def __getitem__(self, i): return self.data[i]
IndexError: list index out of range
>>> conn.close()

I don't know how to fix it so that it works.

Can anyone help me fix that code snippet, or suggest another (better) way
to get the externally visible IP address?


-- 
Steven D'Aprano 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Determine ip address

2005-04-15 Thread Simon Brunning
On 15 Apr 2005 06:03:06 -0700, codecraig <[EMAIL PROTECTED]> 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.1but 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,
[EMAIL PROTECTED],
http://www.brunningonline.net/simon/blog/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Determine ip address

2005-04-15 Thread 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.1but 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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Determine ip address

2005-04-15 Thread Andy Jeffries
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.1but 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Determine ip address

2005-04-15 Thread Donn Cave
In article <[EMAIL PROTECTED]>,
 Simon Brunning <[EMAIL PROTECTED]> wrote:

> On 15 Apr 2005 06:03:06 -0700, codecraig <[EMAIL PROTECTED]> 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, [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Determine ip address

2005-04-15 Thread Lee Harr
On 2005-04-15, codecraig <[EMAIL PROTECTED]> 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.1but 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 = ''

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Determine ip address

2005-04-15 Thread fuzzylollipop
import socket
print socket.gethostbyname( socket.gethostname() )

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Determine ip address

2005-05-05 Thread ontiscal
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.1but i
> want the external IP address (such as ipconfig on windows displays).
> 
> any ideas??
> 
> THanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Determine ip address

2005-05-05 Thread Mike Meyer
[Format recovered from top-posting.]

[EMAIL PROTECTED] 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.1but 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.

  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Binary IP address representation

2009-04-22 Thread Philipp Hagemeister
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA512

Hi Dave,

I've solved this now using ipaddr. ipaddr will be in the stdlib as soon
as its developers realize there are actually not one, but two proposals
to fix the remaining issues waiting for their input.

Anyway, since ipaddr:r68, you can do the following:

>>> import ipaddr
>>> ipaddr.IP('::1').packed
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'

Since you don't know or want to know the family in most use cases, this
interface is actually nicer than that of inet_pton. For the record,
here's how my compatibility function looks like.

def _compat_ipaddr_inet_pton(family, addr):
if family == socket.AF_INET:
return ipaddr.IPv4(addr).packed
elif family == socket.AF_INET6:
return ipaddr.IPv6(addr).packed
else:
raise ValueError("Unknown protocol family " + family)

Since socket.AF_INET6 will not be defined on old systems (are there any
versions of Python 2.5+ that do not have that defined?), your solution
is better for those (but needs netaddr, which is unlikely to enter
stdlib soon). Thanks for sharing it.

Regards,

Philipp

DrKJam wrote:
> Hi,
> 
> I've only just come across this thread this morning :-
> 
> http://mail.python.org/pipermail/python-list/2009-March/703388.html
> 
> Bit late to respond on this list so here is another option (if you are
> still interested).
> 
> Try the netaddr.fallback module :-
> 
 from netaddr.fallback import inet_pton, AF_INET6
 inet_pton(AF_INET6, '::1')
> \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01
> 
> It is self contained and written in pure Python. Forms part of the
> latest 0.6.2 release of netaddr.
> 
> Apologies the code in my project comes across as unreadable :-(
> 
> Regards,
> 
> Dave M.
> 
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEAREKAAYFAknu/zAACgkQ9eq1gvr7CFwUwwCfQLP+dnOdjn9JEttcaFQb5FH0
hLQAn33Lve8k/HXVsW0j7JZP3dL7897W
=ki8e
-END PGP SIGNATURE-
--
http://mail.python.org/mailman/listinfo/python-list


Re: Get Cliet IP Address

2009-08-02 Thread eliasf

"Fred Atkinson" wrote:

What is the function to obtain the client browser's IP
address?


Do you mean the external public IP to the Internet? When I wanted to log the 
dynamic IP that my ADSL connection gets, I used whatismyip.com like this:


   import urllib2

   QUERY_URL = 'http://www.whatismyip.com/automation/n09230945.asp'

   def query():
   """Get IP as a string.

   As per the whatismyip.com automation rules, this function should not
   be called more ofter than every 5 minutes.
   """
   return urllib2.urlopen(QUERY_URL).read()

There's probably a better way, but I'm not very good with networking.  :o) 


--
http://mail.python.org/mailman/listinfo/python-list


Re: Get Cliet IP Address

2009-08-02 Thread Piet van Oostrum
> Fred Atkinson  (FA) wrote:

>FA>What is the function to obtain the client browser's IP
>FA> address?   

You mean in a web server?

The following should work (and was posted by me not long ago):

from os import getenv

ip = (getenv("HTTP_CLIENT_IP") or
  getenv("HTTP_X_FORWARDED_FOR") or
  getenv("HTTP_X_FORWARDED_FOR") or
  getenv("REMOTE_ADDR") or
  "UNKNOWN")

I use getenv() rather than environ[] to avoid exceptions.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


SimpleXMLRPCServer and client IP address

2006-06-28 Thread Jeremy Monnet
Hello,

I've started python a few weeks ago, and to now everything went fine
with my cookbook and a learning book.

Now, I've tried the SimpleXMLRPCServer, and it worked OK untill I
tried to get the client IP address. I have searched a long time the
Internet but couldn't find a _simple_ solution :-)

#Code

from liste_films import *
import SimpleXMLRPCServer

def isOpen():
   # Here I want to get the clien IP address
   if CheckPerms(IP):
   return True
   else:
   return False

if __name__ == '__main__':
   server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8000))
   server.register_function(isOpen)
   server.serve_forever()

#end code

Tips I've found were :
- inherit from requestDispatcher and overload its methods. But that's
not that Simple.
- use the requestHandler and its method address_string(), but I didn't
an easy to understand example
- http://mail.python.org/pipermail/python-list/2006-May/340266.html
but this thread seems not to have been finished :-(

Furthermore, I think I should be able to access the socket object from
where I am (at starting of isOpen() ), but I don't know how. "self"
and "parent" are not defined, I can access the "server" object, but it
says the other end s not connected ("transport endpoint"). I think
this SimpleXMLRPCServer was not threaded (because it says in the API :
"answer all requests one at a time"), so I don't understand why in the
middle of my function the server.socket.getpeername() says it's not
connected.

I'm using python2.3 on linux (debian sid).

Thanks for any help !

Jeremy
-- 
Linux Registered User #317862
Linux From Scratch Registered User #16571
Please do not send me .doc, .xls, .ppt, as I will *NOT* read them.
Please send me only open formats, as OpenDocument or pdf.
-- 
http://mail.python.org/mailman/listinfo/python-list


Getting externally-facing IP address?

2006-11-10 Thread Michael B. Trausch




Hello,

Every programming example that I have seen thus far shows simple server code and how to bind to a socket--however, every example binds to the localhost address.  What I am wondering is this:  Is there a clean way to get the networked IP address of the machine the code is running on?  For example, my laptop's IP address is 192.168.0.101, and I want to bind a server to that address.  Is there a clean way of doing so that will work, for example, when I move the code to my server (which obviously doesn't have the same IP address)?

Thanks in advance.

    -- Mike




--



Michael B. Trausch


[EMAIL PROTECTED]




Phone: (404) 592-5746


Jabber IM: [EMAIL PROTECTED]




Demand Freedom!  Use open and free protocols, standards, and software!





-- 
http://mail.python.org/mailman/listinfo/python-list

Re: IP address of webserver

2007-01-27 Thread Paul Rubin
"Johny" <[EMAIL PROTECTED]> writes:
> So I know  that www.google.com has 209.85.129.147  IP address.
> But how can I find it directly from Python script?

>>> import socket
>>> print socket.gethostbyname('www.google.com')
66.102.7.147
>>> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address of webserver

2007-01-27 Thread Gabriel Genellina
"Johny" <[EMAIL PROTECTED]> escribió en el mensaje 
news:[EMAIL PROTECTED]

> How can I find server's IP address?
>>From console I can use ping, for example:
>
> C:\RobotP\cgi-bin>ping www.google.com
> Pinging www.google.com [209.85.129.147] with 32 bytes of data:
> [...]
> But how can I find it directly from Python script?

py> import socket
py> help(socket.gethostbyname)
Help on built-in function gethostbyname in module _socket:

gethostbyname(...)
gethostbyname(host) -> address

Return the IP address (a string of the form '255.255.255.255') for a 
host.

py> socket.gethostbyname("www.google.com")
'209.85.165.147'

-- 
Gabriel Genellina 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting external IP address

2007-03-05 Thread Duncan Booth
Steven D'Aprano <[EMAIL PROTECTED]> wrote:

> 
>>>> from httplib import HTTPConnection
>>>> from xml.dom.ext.reader.Sax import FromXmlStream
>>>> conn = HTTPConnection('xml.showmyip.com')
>>>> conn.request('GET', '/')
>>>> doc = FromXmlStream(conn.getresponse())
>>>> print doc.getElementsByTagName('ip')[0].firstChild.data
> Traceback (most recent call last):
>   File "", line 1, in ?
>   File "/usr/lib/python2.4/UserList.py", line 28, in __getitem__
> def __getitem__(self, i): return self.data[i]
> IndexError: list index out of range
>>>> conn.close()
> 
> I don't know how to fix it so that it works.
> 
> Can anyone help me fix that code snippet, or suggest another (better) 
way
> to get the externally visible IP address?

Try running it interactively and looking at the data you receive:

>>> conn = HTTPConnection('xml.showmyip.com')
>>> conn.request('GET', '/')

>>> resp = conn.getresponse()
>>> print resp

>>> data = resp.read()
>>> print data
Object moved

Object moved to http://www.showmyip.com/xml/";>here.





If you try connecting to 'www.showmyip.com' and requesting '/xml/' it 
should work.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting external IP address

2007-03-05 Thread Paul Rubin
Duncan Booth <[EMAIL PROTECTED]> writes:
> If you try connecting to 'www.showmyip.com' and requesting '/xml/' it 
> should work.

If the firewall is really obnoxious, it can bounce consecutive queries
around between multiple originating IP addresses.  That is uncommon
but it's been done from time to time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting external IP address

2007-03-05 Thread Duncan Booth
Paul Rubin  wrote:

> Duncan Booth <[EMAIL PROTECTED]> writes:
>> If you try connecting to 'www.showmyip.com' and requesting '/xml/' it 
>> should work.
> 
> If the firewall is really obnoxious, it can bounce consecutive queries
> around between multiple originating IP addresses.  That is uncommon
> but it's been done from time to time.

Yes, each connection through the firewall needs to have a unique 
originating ip and port number. If there are too many machines inside the 
firewall then you may need to allocate multiple ip addresses on the 
outside. I would hope that in general a single internal IP should map to 
one external IP at a time (otherwise you would have problems with ip based 
session persistence connecting to load balanced systems), but you might 
expect to bounce round different IPs after periods of inactivity.

Also you could have multiple levels of NAT in which case the question 
becomes whether Steven wants the IP as seen from outside the immediate 
firewall or outside the final one. Maybe he should be using IPv6 to avoid 
all this?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting external IP address

2007-03-06 Thread Steven D'Aprano
On Mon, 05 Mar 2007 09:02:44 +, Duncan Booth wrote:

> Try running it interactively and looking at the data you receive:
> 
 conn = HTTPConnection('xml.showmyip.com')
 conn.request('GET', '/')
> 
 resp = conn.getresponse()
 print resp
> 
 data = resp.read()
 print data
> Object moved
> 
> Object moved to http://www.showmyip.com/xml/";>here.
> 
> 
> 

Ah! That's the clue I needed -- thanks.

> If you try connecting to 'www.showmyip.com' and requesting '/xml/' it 
> should work.

Thank you muchly! That seems to do the trick.


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting external IP address

2007-03-06 Thread Sion Arrowsmith
Steven D'Aprano  <[EMAIL PROTECTED]> wrote:
>I have a PC behind a firewall, and I'm trying to programmatically
>determine the IP address visible from outside the firewall.
>  [ ... ]
>Can anyone help me fix that code snippet, or suggest another (better) way
>to get the externally visible IP address?

Depending on your circumstances, it may be possible to just ask the
firewall. You'll probably need some kind of administrator login, and
may well have to parse HTML if it's only got a web interface, but it
does mean that you don't have to connect to anything in the outside
world.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Getting external IP address

2007-03-06 Thread Cousin Stanley

> I have a PC behind a firewall, and I'm trying to programmatically
> determine the IP address visible from outside the firewall.
> 

Steven  

  Following is another alternative that might at least
  be worth consideration 

  I use the  lynx  command shown as a command-line alias
  under Debian linux 
 
>>>
>>> import os
>>>
>>> pipe_in = os.popen( 'lynx --dump http://checkip.dyndns.org' )
>>>
>>> ip_addr = pipe_in.readlines()
>>>
>>> for this in ip_addr :
... print this
...


   Current IP Address: 65.39.92.38


-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona


== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet 
News==
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ 
Newsgroups
= East and West-Coast Server Farms - Total Privacy via Encryption =
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting external IP address

2007-03-06 Thread Sergio Correia
The above suggestions seem nice, but I find this one easier:

import urllib2
ext_ip = urllib2.urlopen('http://whatismyip.org/').read()
print ext_ip

The nice thing about the above code is that http://whatismyip.org/
only contains exactly what you want (the ip, nothing more, nothing
less), so no parsing is necessary

Best,
Sergio

On 3/6/07, Cousin Stanley <[EMAIL PROTECTED]> wrote:
>
> > I have a PC behind a firewall, and I'm trying to programmatically
> > determine the IP address visible from outside the firewall.
> > 
>
> Steven 
>
>   Following is another alternative that might at least
>   be worth consideration 
>
>   I use the  lynx  command shown as a command-line alias
>   under Debian linux 
>
> >>>
> >>> import os
> >>>
> >>> pipe_in = os.popen( 'lynx --dump http://checkip.dyndns.org' )
> >>>
> >>> ip_addr = pipe_in.readlines()
> >>>
> >>> for this in ip_addr :
> ... print this
> ...
>
>
>Current IP Address: 65.39.92.38
>
>
> --
> Stanley C. Kitching
> Human Being
> Phoenix, Arizona
>
>
> == Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet 
> News==
> http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ 
> Newsgroups
> = East and West-Coast Server Farms - Total Privacy via Encryption =
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting external IP address

2007-03-06 Thread Steven D'Aprano
On Tue, 06 Mar 2007 14:40:37 -0500, Sergio Correia wrote:

> The above suggestions seem nice, but I find this one easier:
[snip]


Thanks to everybody who replied, that's great.


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Windows getting local ip address

2006-03-22 Thread SolaFide
On Linux, it is a simple matter to get the local ip address with
system.os("ifconfig >> /tmp/ip"); ip=open("/tmp/ip").readlines(), etc.
How can I do this with Windows?

-- 
http://mail.python.org/mailman/listinfo/python-list


Obtaining the remote ip address

2006-03-27 Thread Alvin A. Delagon
One quick question:

I have a python cgi script running behind a CGI server which is also 
built using python using CGIHTTPServer module. How can my cgi script 
obtain the remote ip address?
-- 
http://mail.python.org/mailman/listinfo/python-list


finding IP address of computer

2006-04-27 Thread Chris
How do I find and print to screen the IP address of the computer my
python program is working on?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SimpleXMLRPCServer and client IP address

2006-06-28 Thread Fredrik Lundh
Jeremy Monnet wrote:

> Tips I've found were :
> - use the requestHandler and its method address_string(), but I didn't
> an easy to understand example
> - http://mail.python.org/pipermail/python-list/2006-May/340266.html
> but this thread seems not to have been finished :-(

maybe the explanation in that message was good enough for the poster ?

Your handler object should be getting set up with the client_address 
property.
If not you need to show us some small subset of your app that demonstrates 
the
issue.

You can also inherit from SimpleXMLRPCServer and override verify_request, 
from
BaseServer, if you want the option of blocking requests based on source
address.

the second alternative should be straightforward enough:

class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
def verify_request(self, request, client_address):
return CheckPerms(client_address)

server = MyXMLRPCServer(...)
...

if you need more control, you need to subclass SimpleXMLRPCHandler instead.

> Furthermore, I think I should be able to access the socket object from
> where I am (at starting of isOpen() ), but I don't know how.

you cannot.  the isOpen() function is your RPC handler, and it only sees things 
provided
by the client.

> "self" and "parent" are not defined, I can access the "server" object, but it
> says the other end s not connected ("transport endpoint"). I think
> this SimpleXMLRPCServer was not threaded (because it says in the API :
> "answer all requests one at a time"), so I don't understand why in the
> middle of my function the server.socket.getpeername() says it's not
> connected.

because the server socket isn't the same thing as the client socket -- the 
server socket
is only used to listen for incoming connections; it creates a new client socket 
for each
incoming request.

 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SimpleXMLRPCServer and client IP address

2006-06-28 Thread Jeremy Monnet
Thanks for your answer !

On 6/28/06, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> maybe the explanation in that message was good enough for the poster ?

I think so ... unfortunately not for me ... yet ! :-)

>
> Your handler object should be getting set up with the client_address 
> property.
> If not you need to show us some small subset of your app that 
> demonstrates the
> issue.
>
> You can also inherit from SimpleXMLRPCServer and override verify_request, 
> from
> BaseServer, if you want the option of blocking requests based on source
> address.
>
> the second alternative should be straightforward enough:
>
> class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
> def verify_request(self, request, client_address):
> return CheckPerms(client_address)
>
> server = MyXMLRPCServer(...)
> ...
>
> if you need more control, you need to subclass SimpleXMLRPCHandler instead.


this seems easy, but I'm not sure I could use it : I already provide
more than one method (of course not in the snippet of code I provided,
my mistake probably not to show it) and I plan to add several more in
the near future. Overloading verify_request() in this way means I
can't use the same xmlrpcserver for other methods ? Or am I just wrong
? (I should probably have a look at "request", maybe that's a key ?)
Or is it the "more control" you talked about ?

Jeremy
-- 
Linux Registered User #317862
Linux From Scratch Registered User #16571
Please do not send me .doc, .xls, .ppt, as I will *NOT* read them.
Please send me only open formats, as OpenDocument or pdf.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting externally-facing IP address?

2006-11-10 Thread Tim Williams
On 10/11/06, Michael B. Trausch <[EMAIL PROTECTED]> wrote:



  
  



Every programming example that I have seen thus far shows simple server code and how to bind to a socket--however, every example binds to the localhost address.  What I am wondering is this:  Is there a clean way to get the networked IP address of the machine the code is running on?  For example, my laptop's IP address is 
192.168.0.101, and I want to bind a server to that address.  Is there a clean way of doing so that will work, for example, when I move the code to my server (which obviously doesn't have the same IP address)?


Try  using 0.0.0.0  as the IP address,  or possibly giving IP address at all.   HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Getting externally-facing IP address?

2006-11-10 Thread Tim Williams
On 10/11/06, Tim Williams <[EMAIL PROTECTED]> wrote:
>
>
> On 10/11/06, Michael B. Trausch <[EMAIL PROTECTED]> wrote:
> >
> >
> >
> > Every programming example that I have seen thus far shows simple server
> code and how to bind to a socket--however, every example binds to the
> localhost address.  What I am wondering is this:  Is there a clean way to
> get the networked IP address of the machine the code is running on?  For
> example, my laptop's IP address is 192.168.0.101, and I want to bind a
> server to that address.  Is there a clean way of doing so that will work,
> for example, when I move the code to my server (which obviously doesn't have
> the same IP address)?
> >
> >
>
> Try  using 0.0.0.0  as the IP address,  or possibly giving IP address at
> all.
>
> HTH :)
>
>

Correction:

-> or possibly giving *no* IP address at
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting externally-facing IP address?

2006-11-10 Thread Laszlo Nagy
Michael B. Trausch wrote:
> Hello,
>
> Every programming example that I have seen thus far shows simple 
> server code and how to bind to a socket--however, every example binds 
> to the localhost address.  What I am wondering is this:  Is there a 
> clean way to get the networked IP address of the machine the code is 
> running on?  For example, my laptop's IP address is 192.168.0.101, and 
> I want to bind a server to that address.  Is there a clean way of 
> doing so that will work, for example, when I move the code to my 
> server (which obviously doesn't have the same IP address)?
Another way would be this:

1. connect to a server using a TCP socket (for example, google.com or 
anything else that goes through your desired interface)
2. after connection, read the IP address (client address) from the 
socket object

IMHO if you wish to operate a server, you should either bind to all IP 
addresses, or know your "real" IP address.

   Laszlo

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mulig SPAM: Re: IP address

2007-01-28 Thread Scripter47
Klaus Alexander Seistrup skrev:
> Scripter47 wrote:
> 
>> How do i get my ip address?
>>
>> in cmd.exe i just type "ipconfig" then it prints:
>>   ...
>>   IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
>>   ...
>> how can i do that in python??
> 
> #v+
> 
> python -c 'import re, urllib; print re.findall("Your IP: 
> (.+?)", urllib.urlopen("http://myip.dk/";).read())[0]'
> 
> #v-
> 
> Cheers,
> 
Hmm then you need Internet connecting. can i do it without that? ´

and it doesn't work either :(



-- 
http://mail.python.org/mailman/listinfo/python-list


obtain client ip address from SimpleXMLRPCServer ?

2006-01-23 Thread stuff
Is it possible to obtain the client's ip address from a
SimpleXMLRPCServer instance or subclass instance?  When running
SimpleXMLRPCServer with logRequests = 1, the xmlrpc server prints out
the fqdn on the console, however, I'm not sure if this information
(either fqdn or ip address) is available to the SimpleXMLRPCServer
instance.  Can somebody shed some light on how to obtain this
information?

Thanks,

Phil

-- 
http://mail.python.org/mailman/listinfo/python-list


Controlling source IP address within urllib2

2005-06-03 Thread Dan
Does anybody know how to control the source IP address (IPv4) when
using the urllib2 library?  I have a Linux box with several IP
addresses in the same subnet, and I want to simulate several
individuals within that subnet accessing web pages independently.  I
need the functionality of urllib2 because there will be redirects and
other HTTP-type functions to implement.  It would be nice if I could
create (and bind) sockets myself and then tell the urllib functions to
use those sockets.  Perhaps there is some sort of "back-door" way of
doing this??? Any hints are appreciated!
-Dan

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows getting local ip address

2006-03-22 Thread Fredrik Lundh
"SolaFide" wrote:

> On Linux, it is a simple matter to get the local ip address with
> system.os("ifconfig >> /tmp/ip"); ip=open("/tmp/ip").readlines(), etc.

ip = os.popen("ifconfig").readlines()

is a bit more convenient.

> How can I do this with Windows?

the command is called "ipconfig" in windows.

there's also

>>> import socket
>>> socket.gethostbyname(socket.gethostname())
'1.2.3.4'
>>> socket.gethostbyname_ex(socket.gethostname())
('bender.shiny.com', ['bender'], ['1.2.3.4'])
>>> socket.getaddrinfo(socket.gethostname(), 0)
[(2, 1, 0, '', ('1.2.3.4', 0)), (2, 2, 0, '', ('1.2.3.4', 0))]

etc.  if you're behind a firewall/NAT etc and you want your "public IP",
you can do something like:

>>> import re, urllib
>>> ip = urllib.urlopen('http://checkip.dyndns.org').read()
>>> re.search("(\d+\.\d+\.\d+.\d+)", ip).group()
'4.3.2.1'

hope this helps!





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows getting local ip address

2006-03-22 Thread utabintarbo
You can do essentially the same thing substituting "ipconfig" for
ifconfig. 

Though I am sure there are better ways

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows getting local ip address

2006-03-22 Thread Arne Ludwig
The second solution can give really weird results though, e.g. on my
Linux system I get:

>>> gethostbyaddr(gethostname())
('linux.site', ['linux'], ['127.0.0.2'])

A more flexible but potentially unportable way would be:

>>> import socket
>>> import fcntl
>>> import struct
>>>
>>> def get_ip_address(ifname):
... s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
... return socket.inet_ntoa(fcntl.ioctl(
... s.fileno(),
... 0x8915,  # SIOCGIFADDR
... struct.pack('256s', ifname[:15])
... )[20:24])
...
>>> get_ip_address('eth0')
'192.168.0.174'

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows getting local ip address

2006-03-23 Thread Erno Kuusela
The traditional right way (tm) to do this is to call getsockname() on
the (a?) socket that's connected to the guy you want to tell your
address to. This picks the right address in case you have several. If
you don't have a socket handy, you can make a connectionless UDP
socket and connect() it to a suitable place - this won't result in any
packets on the wire. NAT breaks it of course, but then you couldn't
easily be contacted from outside the NAT anyway.

  -- erno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows getting local ip address

2006-03-23 Thread Arne Ludwig
That man is a genius:

>>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> s.connect(("gmail.com",80))
>>> print s.getsockname()
('192.168.0.174', 2768)
>>> s.close()

Should work on Windows as well.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Obtaining the remote ip address

2006-03-27 Thread Justin Ezequiel
os.environ['REMOTE_ADDR']
os.environ['REMOTE_HOST']

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Obtaining the remote ip address

2006-03-27 Thread Ben Finney
"Alvin A. Delagon" <[EMAIL PROTECTED]> writes:

> I have a python cgi script running behind a CGI server which is also 
> built using python using CGIHTTPServer module. How can my cgi script 
> obtain the remote ip address?

The CGI specification lists a number of environment variables that are
set for the scrit receiving the request. The one you want is
REMOTE_ADDR, and possibly REMOTE_HOST.

http://hoohoo.ncsa.uiuc.edu/cgi/env.html>

Python makes environment variables available via the 'os.environ' map.

http://docs.python.org/lib/os-procinfo.html>

-- 
 \ "I cannot conceive that anybody will require multiplications at |
  `\the rate of 40,000 or even 4,000 per hour ..."  -- F. H. Wales |
_o__)   (1936) |
Ben Finney

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Obtaining the remote ip address

2006-03-27 Thread Alvin A. Delagon
Thanks a lot Justin! ^_^

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Obtaining the remote ip address

2006-03-28 Thread EP
While on the subject of network identity, does anyone have a scheme to 
get the MAC address of the end device?  I've never come up with a way to 
do it, but I haven't had it proven to me that it is impossible (yet).


Justin Ezequiel wrote:

>os.environ['REMOTE_ADDR']
>os.environ['REMOTE_HOST']
>
>  
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Obtaining the remote ip address

2006-03-28 Thread Peter Hansen
EP wrote:
> While on the subject of network identity, does anyone have a scheme to 
> get the MAC address of the end device?  I've never come up with a way to 
> do it, but I haven't had it proven to me that it is impossible (yet).

Which end?  I'll assume you mean _not_ the server end. :-)

The MAC address for the client end (i.e. the end you don't control) 
isn't going to be available to you unless it's local to your network. 
If that's the case, one option is parsing the output of the "arp" 
command as appropriate for your platform (note: this is inherently a 
platform-specific issue).

If the other end is not local, it's unclear what value the MAC address 
would have for you.  Maybe describing that will lead to some more 
effective solutions being suggested.

-Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding IP address of computer

2006-04-27 Thread Gregor Horvath
Chris schrieb:
> How do I find and print to screen the IP address of the computer my
> python program is working on?
> 

IP adresses are bound to network interfaces not to computers.
One Computer can have multiple network interfaces.

-- 
  Servus, Gregor
  http://www.gregor-horvath.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding IP address of computer

2006-04-27 Thread BartlebyScrivener
One way:

>>> import socket
>>> socket.getaddrinfo(socket.gethostname(), None)[0][4][0]

It was the first google hit

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding IP address of computer

2006-04-27 Thread Chris
hehe, works a charm, cheers mate.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding IP address of computer

2006-04-27 Thread Jorge Godoy
Chris wrote:

> hehe, works a charm, cheers mate.

Beware that if you have a different entry in your hosts file you can match a
different name.

Test it:

- add "127.0.0.2yourhost.yourdomain yourhost" to /etc/hosts
- rerun the code.

You'll see "127.0.0.2" as the result.  So take that into account.

-- 
Jorge Godoy  <[EMAIL PROTECTED]>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding IP address of computer

2006-04-27 Thread sturlamolden
print '127.0.0.1'

:-P

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding IP address of computer

2006-04-27 Thread Grant Edwards
On 2006-04-27, Gregor Horvath <[EMAIL PROTECTED]> wrote:
> Chris schrieb:
>> How do I find and print to screen the IP address of the computer my
>> python program is working on?
>> 
>
> IP adresses are bound to network interfaces not to computers.
> One Computer can have multiple network interfaces.

And each interface can have any number if IP addresses
(including none).

-- 
Grant Edwards   grante Yow!  I feel like a wet
  at   parking meter on Darvon!
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding IP address of computer

2006-04-27 Thread Terry Reedy

"Grant Edwards" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> On 2006-04-27, Gregor Horvath <[EMAIL PROTECTED]> wrote:
>> Chris schrieb:
>>> How do I find and print to screen the IP address of the computer my
>>> python program is working on?
>>>
>>
>> IP adresses are bound to network interfaces not to computers.
>> One Computer can have multiple network interfaces.
>
> And each interface can have any number if IP addresses
> (including none).

To answer the OP for typical situations: if you are accessing the internet 
via a local network, the network administrator should be able to tell you. 
In fact, for some networks, the IP address is part of the interface setup. 
If the network is run by a router, it should be able to tell you.  If you 
are sitting behind a NAT (network address translation) router and you want 
to know the external address, there are web pages that echo your externally 
visible address back to you.

Terry Jan Reedy



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding IP address of computer

2006-04-28 Thread DarkBlue
Chris wrote:

> How do I find and print to screen the IP address of the computer my
> python program is working on?

def readip():
 import re, urllib
 f = urllib.urlopen('http://checkip.dyndns.org')
 s = f.read()
 m = re.search('([\d]*\.[\d]*\.[\d]*\.[\d]*)', s)
 return m.group(0)

myip = readip()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding IP address of computer

2006-04-30 Thread Paul Watson
DarkBlue wrote:
> Chris wrote:
> 
> 
>>How do I find and print to screen the IP address of the computer my
>>python program is working on?
> 
> 
> def readip():
>  import re, urllib
>  f = urllib.urlopen('http://checkip.dyndns.org')
>  s = f.read()
>  m = re.search('([\d]*\.[\d]*\.[\d]*\.[\d]*)', s)
>  return m.group(0)
> 
> myip = readip()

IP address and other browser header information available at:

http://xhaus.com/headers
-- 
http://mail.python.org/mailman/listinfo/python-list


Get IP address of WIFI interface

2011-05-13 Thread Far.Runner
Hi Python Experts:
There are two network interfaces on my laptop, one is
100M Ethernet interface, the other is wifi interface, both are connected and
has an IP address. then the question is: how to get the ip address of the
wifi interface in a python script?

OS: Windows or Linux
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IP address to binary conversion

2020-05-08 Thread al . alexiev


Just for the records and to have a fully working bidirectional solution:

>>> ip
'10.44.32.0'
>>> struct.unpack('L', socket.inet_aton(ip))[0]
2108426
>>> socket.inet_ntoa(struct.pack('>>

Good luck ;-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: IP address to binary conversion

2020-05-08 Thread Alan Bawden
al.alex...@gmail.com writes:

> Just for the records and to have a fully working bidirectional solution:
> 
> >>> ip
> '10.44.32.0'
> >>> struct.unpack('L', socket.inet_aton(ip))[0]
> 2108426
> >>> socket.inet_ntoa(struct.pack(' '10.44.32.0'
> >>>
> 
> Good luck ;-)

This will not work as expected on a big-endian machine, because 'L' means
_native_ byte order, but 'https://mail.python.org/mailman/listinfo/python-list


Re: IP address to binary conversion

2020-05-09 Thread al . alexiev
Hi Alan,

Yes, agreed that any '!I' or '!L' combination will work on different type of 
platforms in a consistent manner, when applied in both directions.

I was referring to Alex Martelli's output, where the conversion back to IP 
seems to be reversed, even with '!L', which means that he's already with a 
little-endian byte order and using 'L' or '>> ip = '1.2.168.0'
>>> struct.unpack('L', socket.inet_aton(ip))[0]
11010561
>>> struct.unpack('>> struct.unpack('!L', socket.inet_aton(ip))[0]
16951296
>>>
>>> socket.inet_ntoa(struct.pack('!L',11010561))
'0.168.2.1'
>>> socket.inet_ntoa(struct.pack('>> socket.inet_ntoa(struct.pack('!L',16951296))
'1.2.168.0'
>>>


Greets,
Alex
-- 
https://mail.python.org/mailman/listinfo/python-list


get the IP address of a host

2005-01-05 Thread none
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 correctly it seems.

Can someone enlightened show a light on this:
import socket
def getipaddr(hostname='default'):
"""Given a hostname, perform a standard (forward) lookup and return
a list of IP addresses for that host."""
if hostname == 'default':
hostname = socket.gethostname()
ips = socket.gethostbyname_ex(hostname)[2]
return [i for i in ips if i.split('.')[0] != '127'][0]
It does not seem to work on all hosts. Sometimes socket.gethostbyname_ex 
only retrieves the 127.0.0.x ip adresses of the local loopback. Does 
someone has a more robust solution?

Targetted OS'es are Windows AND linux/unix.
--
http://mail.python.org/mailman/listinfo/python-list


How to learn DNS Server's IP address

2005-09-30 Thread Abdullah Yoldas
How can I learn the DNS Server's IP address for my network, programmatically?

The idea is to learn DNS Server IP and initialize medusa.resolver accordingly.

Thanks in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: obtain client ip address from SimpleXMLRPCServer ?

2006-01-24 Thread Peter Gsellmann
[EMAIL PROTECTED] wrote:

> Is it possible to obtain the client's ip address from a
> SimpleXMLRPCServer instance or subclass instance?  When running
> SimpleXMLRPCServer with logRequests = 1, the xmlrpc server prints out
> the fqdn on the console, however, I'm not sure if this information
> (either fqdn or ip address) is available to the SimpleXMLRPCServer
> instance.  Can somebody shed some light on how to obtain this
> information?
> 
The request-handler has the method: address_string()

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: obtain client ip address from SimpleXMLRPCServer ?

2006-01-26 Thread stuff
Thanks for the reply Peter.  Can you provide a code snippet for
extracting this data.  When I print the dir() of the SimpleXMLRPCServer
instance I do not see a request_handler attribute or method.

Phil

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: obtain client ip address from SimpleXMLRPCServer ?

2006-01-27 Thread Peter Gsellmann
[EMAIL PROTECTED] wrote:

> Thanks for the reply Peter.  Can you provide a code snippet for
> extracting this data.  When I print the dir() of the SimpleXMLRPCServer
> instance I do not see a request_handler attribute or method.
this is ok.
In the serverclass-object, there is no such method because the server
doesn't know which client would connect. It is a long-life object.

Each time a client connects the server creates another sort of object,
the so-called request-handler. This is a short-live object which terminates
when the request is answered. Only this object has knowledge of the client
as it holds the connection.

If you make a subclass of SimpleXMLRPCRequestHandler you can add or
override methods and add a line like this:
print self.address_string()

A sample for such subclassing is given in the file SimpleXMLRPCServer.py
itself. (usually in /usr/lib/python/ )

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: obtain client ip address from SimpleXMLRPCServer ?

2006-01-29 Thread stuff
Thanks again Peter.  I found 2 potential solutions for obtaining the ip
address of the incoming
connection.  The first was to subclass SimpleXMLRPCRequestHandler class
and pass it
to the SimpleXMLRPCServer constructor.  In doing so, I could directly
access the client_address via self.client_address.  This worked just
fine but was a bit overkill.

The other solution I noticed was that SimpleXMLRPCServer's (which
ultimately subclasses BaseServer) handle_request method invokes
get_request (which merely calls self.socket.accept() -- which returns a
tuple including the ip address).  By re-implementing get_request() as
such:

def get_request(self):
req = self.socket.accept()
ip_address = req[1][0]
return req

I can grab the ip address for internal use and have the get_request
method return the expected data for use by hande_request().

Now I just need to find a thread-safe way of passing this data back to
the XMLRPC server's registered_instance.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Controlling source IP address within urllib2

2005-06-04 Thread John J. Lee
"Dan" <[EMAIL PROTECTED]> writes:

> Does anybody know how to control the source IP address (IPv4) when
> using the urllib2 library?  I have a Linux box with several IP
> addresses in the same subnet, and I want to simulate several
> individuals within that subnet accessing web pages independently.  I
> need the functionality of urllib2 because there will be redirects and
> other HTTP-type functions to implement.  It would be nice if I could
> create (and bind) sockets myself and then tell the urllib functions to
> use those sockets.  Perhaps there is some sort of "back-door" way of
> doing this??? Any hints are appreciated!

There's no built-in support, but I expect it's very easy to do.  I
suppose the easiest way would be to derive from
httplib.HTTPConnection, overriding .connect() to call .bind() on the
socket.  Call it BindingHTTPConnection, then define:

class BindingHTTPHandler(urllib2.HTTPHandler):
def http_open(self, req):
return self.do_open(BindingHTTPConnection, req)


Same goes for https:

if hasattr(httplib, 'HTTPS'):
class BindingHTTPSHandler(urllib2.HTTPSHandler):
def https_open(self, req):
return self.do_open(BindingHTTPSConnection, req)


Personally, I probably wouldn't use build_opener(), but since the
above classes derive from the corresponding urllib2 classes, you can
do:

opener = urllib2.build_opener(BindingHTTPHandler, BindingHTTPSHandler)
response = opener.open('http://www.example.com/')


John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Controlling source IP address within urllib2

2005-06-06 Thread Dan
John,
Thanks for your input.  I can kind of see the light in this, but I'm
having difficulty knowing where the "do_open" method comes from.  Also,
I'll need to follow redirects, so I assume then I would add a
HTTPRedirectHandler instance to the urllib2.build_opener. (?)  Thanks
again for your help.
-Dan

-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   >