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


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-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: getfqdn passes a hostname to gethostbyaddr instead of an ip address

2018-09-12 Thread Thomas Jollans

On 12/09/18 16:29, Florian Bergmann wrote:

On the other hand I feel given the documentation, passing the `ip_address` would
be the right thing to do, so I am wondering if I am missing something very
obvious here (especially given that the code seems to be unchanged for 18 
years).
Whatever the docs say, turning the hostname into an IP address and 
working with that would be incorrect.


Say we have a server, 'fred.weasley.example.com', which is also known as 
'www.example.com'. Its reverse DNS pointer is 
'fred.weasley.example.com'. Now, if we have 'example.com' on our DNS 
search path, the FQDN of 'www' is 'www.example.com', while the FQDN 
derived from the IP would be 'fred.weasley.example.com'.


Right?
--
https://mail.python.org/mailman/listinfo/python-list


Re: getfqdn passes a hostname to gethostbyaddr instead of an ip address

2018-09-12 Thread Rhodri James

On 12/09/18 15:29, Florian Bergmann wrote:

Hello,

While I was debugging some salt issues I dug into the python code and found a
piece of code in the `socket.py` module that surprised my a bit:

In the `getfqdn` function the `gethostbyaddr` name function is being called with
a `hostname` instead of an `ipaddress`:


[snip]


2. `gethostbyaddr()`:

Also from the documentation:

```
Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the primary 
host name responding to the *given ip_address* (...)
```

As the documentation states it expects an `ip_address` and not a hostname,
but it is given a `hostname` instead.


I believe the online documentation is wrong.  The help text certainly 
differs:


Help on built-in function gethostbyaddr in module _socket:

gethostbyaddr(...)
gethostbyaddr(host) -> (name, aliaslist, addresslist)

Return the true host name, a list of aliases, and a list of IP 
addresses,
for a host.  The host argument is a string giving a host name or IP 
number.



--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


getfqdn passes a hostname to gethostbyaddr instead of an ip address

2018-09-12 Thread Florian Bergmann
Hello,

While I was debugging some salt issues I dug into the python code and found a
piece of code in the `socket.py` module that surprised my a bit:

In the `getfqdn` function the `gethostbyaddr` name function is being called with
a `hostname` instead of an `ipaddress`:

```python
def getfqdn(name=''):
"""Get fully qualified domain name from name.
An empty argument is interpreted as meaning the local host.
First the hostname returned by gethostbyaddr() is checked, then
possibly existing aliases. In case no FQDN is available, hostname
from gethostname() is returned.
"""
name = name.strip()
if not name or name == '0.0.0.0':
name = gethostname() # (1)
try:
hostname, aliases, ipaddrs = gethostbyaddr(name) # (2)
except error:
pass
else:
aliases.insert(0, hostname)
for name in aliases:
if '.' in name:
break
else:
name = hostname
return name
```

1. `gethostname()`:

The documentation states:

```
Return a string containing the hostname of the machine where the Python 
interpreter is currently executing.

If you want to know the current machine’s IP address, you may want to use 
gethostbyname(gethostname()).
```

2. `gethostbyaddr()`:

Also from the documentation:

```
Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the primary 
host name responding to the *given ip_address* (...)
```

As the documentation states it expects an `ip_address` and not a hostname,
but it is given a `hostname` instead.

I used the following two snippets to check the different behaviors:

```
python -c 'import socket; 
print(socket.gethostbyaddr(socket.gethostbyname(socket.gethostname('
```

```
python -c 'import socket; print(socket.gethostbyaddr(socket.gethostname()))'
```

Now on *most* of my machines the results are exactly the same, but on some it
differs (which I actually attribute to strange `/etc/hosts` configurations).

On the other hand I feel given the documentation, passing the `ip_address` would
be the right thing to do, so I am wondering if I am missing something very
obvious here (especially given that the code seems to be unchanged for 18 
years).

Best regards,

Florian
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: urllib/urllib2 support for specifying ip address

2014-06-19 Thread Chris Angelico
On Fri, Jun 20, 2014 at 12:19 AM, Robin Becker  wrote:
> in practice [monkeypatching socket] worked well with urllib in python27.

Excellent! That's empirical evidence of success, then.

Like with all monkey-patching, you need to keep it as visible as
possible, but if your driver script is only a page or two of code, it
should be pretty clear what's going on.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: urllib/urllib2 support for specifying ip address

2014-06-19 Thread Robin Becker

On 19/06/2014 13:03, Chris Angelico wrote:
.

I can use python >= 3.3 if required.


The main reason I ask is in case something's changed. Basically, what
I did was go to my Python 2 installation (which happens to be 2.7.3,
because that's what Debian Wheezy ships with - not sure why it hasn't
been updated beyond that), pull up urllib2.py, and step through
manually, seeing where the hostname gets turned into an IP address.
Hence, this code:

.
in practice this approach worked well with urllib in python27.
--
Robin Becker
--
https://mail.python.org/mailman/listinfo/python-list


Re: urllib/urllib2 support for specifying ip address

2014-06-19 Thread Chris Angelico
On Thu, Jun 19, 2014 at 9:51 PM, Robin Becker  wrote:
>> Since you mention urllib2, I'm assuming this is Python 2.x, not 3.x.
>> The exact version may be significant.
>>
> I can use python >= 3.3 if required.

The main reason I ask is in case something's changed. Basically, what
I did was go to my Python 2 installation (which happens to be 2.7.3,
because that's what Debian Wheezy ships with - not sure why it hasn't
been updated beyond that), pull up urllib2.py, and step through
manually, seeing where the hostname gets turned into an IP address.
Hence, this code:

>> import socket.
>> orig_create_connection = socket.create_connection
>> def create_connection(address, *args, **kwargs):
>>  if address == "domainA": address = "1.2.3.4"
>>  return orig_create_connection(address, *args, **kwargs)
>> socket.create_connection = create_connection
>> # Proceed to use urllib2.urlopen()
>>
>> Untested, but may do what you want.
>>
>
> this seems like a way forward

So if it works, that's great! If it doesn't, and you're on a different
version of Python (2.6? 2.4 even?), you might want to look at
repeating the exercise I did, with your actual Python.

But as a general rule, I'd recommend going with Python 3.x unless you
have a good reason for using 2.x. If a feature's been added to let you
mock in a different IP address, it'll be in 3.something but probably
not in 2.7.

>> Normally, though, I'd look at just changing the hosts file, if at all
>> possible. You're right that it does change state for your whole
>> computer, but it's generally the easiest solution.
>>
>> ChrisA
>>
> me too, but I want to start torturing from about 10 different servers so
> plumbum + a python script seems like a good choice and I would not really
> want to hack the hosts files back and forth on a regular basis.

Fair enough. In that case, the best thing to do would probably be
monkey-patching, with code along the lines of what I posted above.
Make the change as small and as specific as you can.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: urllib/urllib2 support for specifying ip address

2014-06-19 Thread Robin Becker

..


Since you mention urllib2, I'm assuming this is Python 2.x, not 3.x.
The exact version may be significant.


I can use python >= 3.3 if required.



Can you simply query the server by IP address rather than host name?
According to the docs, urllib2.urlopen() doesn't check the
certificate, so it should be accepted. Or does the server insist on
the hostname being correct?

Failing that, you could monkey-patch socket.create_connection, which
seems to be the thing that ultimately does the work. Something like
this:

import socket.
orig_create_connection = socket.create_connection
def create_connection(address, *args, **kwargs):
 if address == "domainA": address = "1.2.3.4"
 return orig_create_connection(address, *args, **kwargs)
socket.create_connection = create_connection
# Proceed to use urllib2.urlopen()

Untested, but may do what you want.



this seems like a way forward



Normally, though, I'd look at just changing the hosts file, if at all
possible. You're right that it does change state for your whole
computer, but it's generally the easiest solution.

ChrisA

me too, but I want to start torturing from about 10 different servers so plumbum 
+ a python script seems like a good choice and I would not really want to hack 
the hosts files back and forth on a regular basis.

--
Robin Becker

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


Re: urllib/urllib2 support for specifying ip address

2014-06-19 Thread Chris Angelico
On Thu, Jun 19, 2014 at 7:22 PM, Robin Becker  wrote:
> I want to run torture tests against an https server on domain A; I have
> configured apache on the server to respond to a specific hostname ipaddress.
>
> I don't want to torture the live server so I have set up an alternate
> instance on a different ip address.

Since you mention urllib2, I'm assuming this is Python 2.x, not 3.x.
The exact version may be significant.

Can you simply query the server by IP address rather than host name?
According to the docs, urllib2.urlopen() doesn't check the
certificate, so it should be accepted. Or does the server insist on
the hostname being correct?

Failing that, you could monkey-patch socket.create_connection, which
seems to be the thing that ultimately does the work. Something like
this:

import socket.
orig_create_connection = socket.create_connection
def create_connection(address, *args, **kwargs):
if address == "domainA": address = "1.2.3.4"
return orig_create_connection(address, *args, **kwargs)
socket.create_connection = create_connection
# Proceed to use urllib2.urlopen()

Untested, but may do what you want.

Normally, though, I'd look at just changing the hosts file, if at all
possible. You're right that it does change state for your whole
computer, but it's generally the easiest solution.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


urllib/urllib2 support for specifying ip address

2014-06-19 Thread Robin Becker
I want to run torture tests against an https server on domain A; I have 
configured apache on the server to respond to a specific hostname ipaddress.


I don't want to torture the live server so I have set up an alternate instance 
on a different ip address.


Is there a way to get urlib or urllib2 to use my host name and a specifed ip 
address?


I can always change my hosts file, but that is inconvenient and potentially 
dangerous.

--
Robin Becker

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


Re: How to send broadcast IP address to network?

2013-08-24 Thread Irmen de Jong
On 23-8-2013 14:32, lightai...@gmail.com wrote:
> I want to send a broadcast packet to all the computers connected to my home 
> router. 
> 
> The following 2 lines of code do not work;
> host="192.168.0.102"
> s.connect((host, port))
> 
> Can someone advise?
> 
> Thank you.
> 

Use UDP (datagram) sockets. Use sendto() and not connect(), because UDP is
connectionless. This should work:

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(b"thedata", 0, ("", ))#  = port
sock.close()


Irmen

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


Re: How to send broadcast IP address to network?

2013-08-23 Thread Chris Angelico
On Fri, Aug 23, 2013 at 10:32 PM,   wrote:
> I want to send a broadcast packet to all the computers connected to my home 
> router.
>
> The following 2 lines of code do not work;
> host="192.168.0.102"
> s.connect((host, port))
>
> Can someone advise?

You can't establish a TCP socket with a broadcast address. That just
doesn't work. Can you please show a whole lot more context so we can
see what's happening here? Thanks!

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


Re: How to send broadcast IP address to network?

2013-08-23 Thread Neil Cerutti
On 2013-08-23, lightai...@gmail.com  wrote:
>> The following 2 lines of code do not work;
>> 
>> host="192.168.0.255"
>> host="192.168.0.102"
>> s.connect((host, port))

Traceback (most recent call last):
  File "", line 1, in 
NameError: name 's' is not defined

I bet that's not the same traceback you get. Furthermore, port
isn't defined either.

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


Re: How to send broadcast IP address to network?

2013-08-23 Thread lightaiyee
Some typo mistake. 
Should be host="192.168.0.255", not "192.168.0.102"

On Friday, August 23, 2013 8:32:10 PM UTC+8, light...@gmail.com wrote:
> I want to send a broadcast packet to all the computers connected to my home 
> router. 
> 
> 
> 
> The following 2 lines of code do not work;
> 
> host="192.168.0.102"
> 
> s.connect((host, port))
> 
> 
> 
> Can someone advise?
> 
> 
> 
> Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


How to send broadcast IP address to network?

2013-08-23 Thread lightaiyee
I want to send a broadcast packet to all the computers connected to my home 
router. 

The following 2 lines of code do not work;
host="192.168.0.102"
s.connect((host, port))

Can someone advise?

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


Re: Geo Location extracted from visitors ip address

2013-07-08 Thread Grant Edwards
On 2013-07-06, ?? Gr33k  wrote:
>  6/7/2013 4:41 , ??/?? ?? Gr33k :
>> Yes i know iam only storing the ISP's city instead of visitor's homeland
>> but this is the closest i can get:
>>
>> try:
>>gi = pygeoip.GeoIP('/home/nikos/GeoLiteCity.dat')
>>city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
>>host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )
>> except Exception as e:
>>host = repr(e)
>>
>>
>> Tried it myself and it falsey said that i'am from Europe/Athens (capital
>> of Greece) while i'am from Europe/Thessaloniki (sub-capital of Greece)
>>
>> If we can pin-point the uvisitor more accurately plz let me know.
>
> Good morning from Greece,
>
> All my Greece visitors as Dave correctly said have the ISP address which 
> here in Greece is Europe/Athens, so i have now way to distinct the 
> cities of the visitors.
>
> Is there any way to pinpoint the visitor's exact location?

No.

-- 
Grant Edwards   grant.b.edwardsYow! Does someone from
  at   PEORIA have a SHORTER
  gmail.comATTENTION span than me?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-08 Thread Grant Edwards
On 2013-07-06, ?? Gr33k  wrote:
> Yes i know iam only storing the ISP's city instead of visitor's homeland 
> but this is the closest i can get:
>
> try:
>gi = pygeoip.GeoIP('/home/nikos/GeoLiteCity.dat')
>city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
>host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )
> except Exception as e:
>host = repr(e)
>
>
> Tried it myself and it falsey said that i'am from Europe/Athens (capital 
> of Greece) while i'am from Europe/Thessaloniki (sub-capital of Greece)
>
> If we can pin-point the uvisitor more accurately plz let me know.

For the Nth time: you can't.

-- 
Grant Edwards   grant.b.edwardsYow! HOORAY, Ronald!!
  at   Now YOU can marry LINDA
  gmail.comRONSTADT too!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-08 Thread Chris Angelico
On Sat, Jul 6, 2013 at 7:18 AM, Dave Angel  wrote:
> On 07/05/2013 04:44 PM, Tim Roberts wrote:
>>
>> ? Gr33k  wrote:
>>>
>>>
>>> Is there a way to extract out of some environmental variable the Geo
>>> location of the user being the city the user visits out website from?
>>>
>>> Perhaps by utilizing his originated ip address?
>>
>>
>> It is possible to look up the geographic region associated with a block of
>> IP addresses.  That does not necessarily bear any resemblence to the
>> actual
>> location of the user.  It tells you the location of the Internet provider
>> that registered the IP addresses.
>>
>
> To be more specific, the last time I checked, my IP address is registered to
> a location over 1000 miles away.  And it's not a dedicated IP address, so it
> could very well be used tomorrow by someone 200 nmiles the other side of the
> registered location.
>
> Further, I sometimes use other ISP's with different addresses, even when I
> don't leave the house.  And once I leave, anything's possible.

That said, though, geolocation is usually accurate enough to report
which country you're in. For basic statistical analysis, that's
generally sufficient.

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


Re: Geo Location extracted from visitors ip address

2013-07-08 Thread Support by Νίκος

Στις 5/7/2013 10:28 μμ, ο/η Jerry Hill έγραψε:

On Fri, Jul 5, 2013 at 3:08 PM, Νίκος Gr33k  wrote:

Is there a way to extract out of some environmental variable the Geo
location of the user being the city the user visits out website from?

Perhaps by utilizing his originated ip address?

No, you'd need to take the originating IP address and look it up in a
geolocation database or submit it to a geolocation service and get the
response back from them.  It's not stored in any environment
variables.

I see but i dont know how to do this Geo location lookups.
Can you give me more specific details please?
--
Webhost <http://superhost.gr>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-08 Thread Robert Kern

On 2013-07-06 09:41, Νίκος Gr33k wrote:

Στις 6/7/2013 11:30 πμ, ο/η Chris Angelico έγραψε:

On Sat, Jul 6, 2013 at 6:01 PM, � Gr33k  wrote:

Is there any way to pinpoint the visitor's exact location?


Yes. You ask them to fill in a shipping address. They may still lie,
or they may choose to not answer, but that's the best you're going to
achieve without getting a wizard to cast Scrying.


No, no registration requirements.

you know when i go to maps.google.com its always find my exact city of location
and not just say Europe/Athens.

and twitter and facebook too both of them pinpoint my _exact_ location.

How are they able to do it? We need the same way.


They use client-side JavaScript. This is a relatively new API available in most, 
but not all, recent browsers. This information will not be available to your CGI 
script. You will have to generate HTML with the proper JavaScript to get the 
geolocation (if the user allows it) and then send it back to your server through 
a different CGI script (or web application endpoint).


  http://diveintohtml5.info/geolocation.html

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Geo Location extracted from visitors ip address

2013-07-08 Thread Jerry Hill
On Fri, Jul 5, 2013 at 3:08 PM, Νίκος Gr33k  wrote:
> Is there a way to extract out of some environmental variable the Geo
> location of the user being the city the user visits out website from?
>
> Perhaps by utilizing his originated ip address?

No, you'd need to take the originating IP address and look it up in a
geolocation database or submit it to a geolocation service and get the
response back from them.  It's not stored in any environment
variables.

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


Re: Geo Location extracted from visitors ip address

2013-07-08 Thread Joel Goldstick
On Fri, Jul 5, 2013 at 3:08 PM, Νίκος Gr33k  wrote:

> Is there a way to extract out of some environmental variable the Geo
> location of the user being the city the user visits out website from?
>
> Perhaps by utilizing his originated ip address?
>
> --
> What is now proved was at first only imagined!
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>

Google shows lots of results.  This one might work for you:
http://freegeoip.net/

-- 
Joel Goldstick
http://joelgoldstick.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-07 Thread Νίκος Gr33k

Στις 6/7/2013 11:51 μμ, ο/η Νίκος Gr33k έγραψε:

Στις 6/7/2013 11:32 μμ, ο/η Tim Chase έγραψε:

Can you be more specific please about using the aforementioned

HTML5 location API ?

https://www.google.com/search?q=html5+location+api

It's client-side JavaScript.



so, i must edit my cgi script and do this:

print '''

var x=document.getElementById("demo");
function getLocation()
   {
   if (navigator.geolocation)
 {
 navigator.geolocation.getCurrentPosition(showPosition);
 }
   else{x.innerHTML="Geolocation is not supported by this browser.";}
   }
function showPosition(position)
   {
   x.innerHTML="Latitude: " + position.coords.latitude +
   "
Longitude: " + position.coords.longitude; } ''' Will that do the trick? but then again i want the city to be stored in the city variable. Somehow the above javascript code mu return me a value that i will the store at variable "city". I don't know how to do that. I had a reply of another person telling me these: Google, Facebook, Microsoft, Amazon and most other gigantic companies with lots of money track you in several different ways, not just by the IP. They compare several categories of tracking to generate a list of possible locations for you and then pick the one with the highest confidence. For example, I have an AU phone. If I register with AU Cloud that also registers me with Google, and then my AU tower, IP and GPS location all get reported to Google. When I login later on a desktop to the same GoogleID account, they only have my IP and tracking cookies to look at, but they already know to check the latest location of my phone -- and whether its turned on/permitting GPS updates right then affects the confidence report % of that method of tracking. Recent reservations, dated product/service reviews, driving directions, map inquiries, map bookmarks/pins, etc. all give some confidence for frequented location and movement history each. Any billing relationship you have with them will give them another tracking point based on your billing address, and they can compare the billing address with frequented GPS locs, past shipping information and recent locale-oriented searches. The more recent the data and the more points of data match the same location the more confidence the potential location has. ...and so on. Its pretty creepy, actually. Anyway, you can't just do this using IP information. To get reliable, live, pinpoint user location data you need to do one of: Convince the user to report/register/pick their location Convince the user to permit you to track their phone Get a contract with Google that buys you their best guess at user location Be like Google and engage in a conspiracy to invade the privacy of millions that dwarfs the resources of most intelligence agencies (and then sell it to intelligence agencies, just like Google does) -- What is now proved was at first only imagined! -- http://mail.python.org/mailman/listinfo/python-list

Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Νίκος Gr33k

Στις 6/7/2013 11:32 μμ, ο/η Tim Chase έγραψε:

Can you be more specific please about using the aforementioned

HTML5 location API ?

https://www.google.com/search?q=html5+location+api

It's client-side JavaScript.



so, i must edit my cgi script and do this:

print '''

var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude +
  "
Longitude: " + position.coords.longitude; } ''' Will that do the trick? but then again i want the city to be stored in the city variable. Somehow the above javascript code mu return me a value that i will the store at variable "city". I don't know how to do that. -- What is now proved was at first only imagined! -- http://mail.python.org/mailman/listinfo/python-list

Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Νίκος Gr33k

Στις 6/7/2013 11:33 μμ, ο/η Tim Chase έγραψε:

Who controlls the script's privileges then?
The process that calls the script file, i.e. Apache?

Yes.


When we run the python interpreter to run a python script like
python metrites.py

then out script will inherit the kind of access the python interpreter 
has which in turn will inherit the kind of access the user that is run 
under upon has?


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Tim Chase
On 2013-07-06 23:12, Νίκος Gr33k wrote:
> I though that the ownership of the script file controlled the
> privileges it runs under.

Only if the script is SUID.  In some environments, scripts
can't be run SUID, only binaries.

> Who controlls the script's privileges then?
> The process that calls the script file, i.e. Apache?

Yes.

-tkc



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


Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Tim Chase
On 2013-07-06 23:14, Νίκος Gr33k wrote:
 Can you be more specific please about using the aforementioned
> HTML5 location API ?

https://www.google.com/search?q=html5+location+api

It's client-side JavaScript.

> Never heard of it. Can it be utilizized via a python cgi script?

Because it's client-side JavaScript, it runs, well, on the client's
browser.  Note that the user may be prompted regarding whether they
want to permit the website to access location information, so this
information may not be available.  If the user permits and JS is
enabled, the client-side JS code can then make AJAX requests (or stash
it in a cookie that gets sent with future requests) to convey the
location information to the server where your Python code is running.

-tkc


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


Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Νίκος Gr33k

Στις 6/7/2013 2:20 μμ, ο/η Tim Chase έγραψε:

1) using the aforementioned HTML5 location API, your device may be
tattling on where you are.  Are you browsing from a smart-phone or
other device with a GPS built in?


I'm using my lenovo laptop, by maps.gogole.com, fb and twitter have no 
problem pionpoint my exact location, even postal code.


How do they do it?

Can you be more specific please about using the aforementioned HTML5 
location API ?


Never heard of it. Can it be utilizized via a python cgi script?



--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Νίκος Gr33k

Στις 6/7/2013 5:43 μμ, ο/η Dennis Lee Bieber έγραψε:

It was some guy form hostgator.com that had told me that a python script
has the same level of access to anything on the filesystem as its
coressponding user running it, implying that if i run it under user
'root' the python script could access anything.

Yes, IF YOU RUN IT UNDER "root"... The ownership of the script file
doesn't control the privileges it runs under as long as the file itself is
read-access to other "users".


I though that the ownership of the script file controlled the privileges 
it runs under.


Who controlls the script's privileges then?
The process that calls the script file, i.e. Apache?


> as the file itself is
> read-access to other "users".

What do you mean by that?
--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Dave Angel

On 07/06/2013 04:41 AM, Νίκος Gr33k wrote:

Στις 6/7/2013 11:30 πμ, ο/η Chris Angelico έγραψε:

On Sat, Jul 6, 2013 at 6:01 PM, � Gr33k  wrote:

Is there any way to pinpoint the visitor's exact location?


Yes. You ask them to fill in a shipping address. They may still lie,
or they may choose to not answer, but that's the best you're going to
achieve without getting a wizard to cast Scrying.


No, no registration requirements.

you know when i go to maps.google.com its always find my exact city of
location and not just say Europe/Athens.

and twitter and facebook too both of them pinpoint my _exact_ location.

How are they able to do it? We need the same way.



At some point, you entered your address, and it's stored in some 
database in the sky.  You have cookies on your machine which correlate 
to that database.


Chances are you did it for google-maps, and google shared it with their 
search engine and other parts.


As far as I know, each such company has a separate database, but perhaps 
google (for exakmple) has an partner API which facebook uses.


--
DaveA

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


Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Tim Chase
On 2013-07-06 11:41, Νίκος Gr33k wrote:
> you know when i go to maps.google.com its always find my exact city
> of location and not just say Europe/Athens.
> 
> and twitter and facebook too both of them pinpoint my _exact_
> location.
> 
> How are they able to do it? We need the same way.

A couple possibilities:

1) using the aforementioned HTML5 location API, your device may be
tattling on where you are.  Are you browsing from a smart-phone or
other device with a GPS built in?

2) at some point in the distant past, you told Google where you are,
and it has dutifully remembered that.  Try using an alternate browser
in a new session (Firefox has the ability to create a new profile;
Chrome/Chromium should have the ability to start up with a virgin
profile; I can't say for Safari or IE) and see if Google suddenly
lacks the ability to locate you

3) Google has a better IP-to-location map database than you have.
You might have to pay real money for such functionality.  Or, you
might have to use a different library, as the IP-to-location database
that I linked you to earlier has both an "IP to Country" and an "IP
to City" database.  Note that this is often wrong or grossly
inaccurate, as mentioned in other threads (geolocation by IP address
often puts me in the nearest major city which is a good 45min drive
away, and if I just visit Google maps with a fresh browser, it just
shows me the state, TX, which is a ~13hr drive across, if done at
65mph the whole way)

-tkc




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


Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Νίκος Gr33k

Στις 6/7/2013 11:30 πμ, ο/η Chris Angelico έγραψε:

On Sat, Jul 6, 2013 at 6:01 PM, � Gr33k  wrote:

Is there any way to pinpoint the visitor's exact location?


Yes. You ask them to fill in a shipping address. They may still lie,
or they may choose to not answer, but that's the best you're going to
achieve without getting a wizard to cast Scrying.


No, no registration requirements.

you know when i go to maps.google.com its always find my exact city of 
location and not just say Europe/Athens.


and twitter and facebook too both of them pinpoint my _exact_ location.

How are they able to do it? We need the same way.

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Chris Angelico
On Sat, Jul 6, 2013 at 6:01 PM, Νίκος Gr33k  wrote:
> Is there any way to pinpoint the visitor's exact location?

Yes. You ask them to fill in a shipping address. They may still lie,
or they may choose to not answer, but that's the best you're going to
achieve without getting a wizard to cast Scrying.

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


Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Νίκος Gr33k

Στις 6/7/2013 4:41 πμ, ο/η Νίκος Gr33k έγραψε:

Yes i know iam only storing the ISP's city instead of visitor's homeland
but this is the closest i can get:

try:
   gi = pygeoip.GeoIP('/home/nikos/GeoLiteCity.dat')
   city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
   host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )
except Exception as e:
   host = repr(e)


Tried it myself and it falsey said that i'am from Europe/Athens (capital
of Greece) while i'am from Europe/Thessaloniki (sub-capital of Greece)

If we can pin-point the uvisitor more accurately plz let me know.


Good morning from Greece,

All my Greece visitors as Dave correctly said have the ISP address which 
here in Greece is Europe/Athens, so i have now way to distinct the 
cities of the visitors.


Is there any way to pinpoint the visitor's exact location?


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-06 Thread Νίκος Gr33k

Στις 6/7/2013 5:52 πμ, ο/η Dennis Lee Bieber έγραψε:

On Sat, 06 Jul 2013 04:10:24 +0300, ? Gr33k 
declaimed the following:



But he cgi scripts when running have full access to the server.
No? or they only have the kind of access that their user has also?


In any decent system, the web server runs as a particular user, and
only has access to the web content and scripts. And those scripts run as
the web server process (at most -- it may be that they run at an even more
restricted mode).

So NO, they do NOT have access to stuff under /root; for ancient
CGI-BIN style, they may be restricted to only the files in the CGI-BIN
directory.


Thats why i was getting permission denied vene when i had +x
when i moved the geo.dat file to /home/nikos/geo.dat then the cgi python 
script was able to opened it.


It was some guy form hostgator.com that had told me that a python script 
has the same level of access to anything on the filesystem as its 
coressponding user running it, implying that if i run it under user 
'root' the python script could access anything.


Are you sure that python scripts run under Apache user or Nobody user in 
my case and not as user 'nikos' ?


Is there some way to test that?

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Νίκος Gr33k
Yes i know iam only storing the ISP's city instead of visitor's homeland 
but this is the closest i can get:


try:
  gi = pygeoip.GeoIP('/home/nikos/GeoLiteCity.dat')
  city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
  host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )
except Exception as e:
  host = repr(e)


Tried it myself and it falsey said that i'am from Europe/Athens (capital 
of Greece) while i'am from Europe/Thessaloniki (sub-capital of Greece)


If we can pin-point the uvisitor more accurately plz let me know.



--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Joel Goldstick
On Fri, Jul 5, 2013 at 9:10 PM, Νίκος Gr33k  wrote:

> Στις 6/7/2013 3:56 πμ, ο/η Joel Goldstick έγραψε:
>
>>
>> Your code is not finding /root/GeoIPCity.dat because your directory has
>> this file: GeoLiteCity.dat
>>
>> FileNotFoundError: [Errno 2] No such file or directory:
>> '/root/GeoIPCity.dat'
>>
>
> My mistake.
> Is there a differnce between GeoLiteCity.dat and GeoIPCity.dat
>
>
>  Aside from that you might have some permission problems since the file
>> is owned by root.
>>
>
> But he cgi scripts when running have full access to the server.
> No? or they only have the kind of access that their user has also?
>
>
>
>
>  As was also pointed out, you only get information about where your isp
>> is located.
>>
> Its the best i can get to, since there is no other way to match the users
> city.
>
> Β  Phones and tablets find location from triangulating cell
>
>> towers.Β  I don't think that laptops have that capability, and desktops
>> probably even less likely.
>>
>
> What do you mean by that?
>



>
>  What is the purpose that you wish to serve.Β  I don't think you've
>> thought this through.
>>
>
> I just dont want to store visitor's ip addresses any more, i prefer to
> store its city of origin.
>
> Except its the ISP city that you are getting, not the user's

>
>
> --
> What is now proved was at first only imagined!
> --
> http://mail.python.org/**mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Νίκος Gr33k

Στις 6/7/2013 3:56 πμ, ο/η Joel Goldstick έγραψε:


Your code is not finding /root/GeoIPCity.dat because your directory has
this file: GeoLiteCity.dat

FileNotFoundError: [Errno 2] No such file or directory:
'/root/GeoIPCity.dat'


My mistake.
Is there a differnce between GeoLiteCity.dat and GeoIPCity.dat


Aside from that you might have some permission problems since the file
is owned by root.


But he cgi scripts when running have full access to the server.
No? or they only have the kind of access that their user has also?




As was also pointed out, you only get information about where your isp
is located.
Its the best i can get to, since there is no other way to match the 
users city.


Β  Phones and tablets find location from triangulating cell

towers.Β  I don't think that laptops have that capability, and desktops
probably even less likely.


What do you mean by that?


What is the purpose that you wish to serve.Β  I don't think you've
thought this through.


I just dont want to store visitor's ip addresses any more, i prefer to 
store its city of origin.



--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Joel Goldstick
On Fri, Jul 5, 2013 at 8:08 PM, Νίκος Gr33k  wrote:

> Στις 6/7/2013 2:58 πμ, ο/η Νίκος Gr33k έγραψε:
>
>  Στις 6/7/2013 2:55 πμ, ο/η Νίκος Gr33k έγραψε:
>>
>>> Στις 5/7/2013 10:58 μμ, ο/η Tim Chase έγραψε:
>>>
>>>> On 2013-07-05 22:08, Νίκος Gr33k wrote:
>>>>
>>>>> Is there a way to extract out of some environmental variable the
>>>>> Geo location of the user being the city the user visits out website
>>>>> from?
>>>>>
>>>>> Perhaps by utilizing his originated ip address?
>>>>>
>>>>
>>>> Yep.  You can get an 11MB database (17MB uncompressed)
>>>>
>>>> http://dev.maxmind.com/geoip/**legacy/downloadable/<http://dev.maxmind.com/geoip/legacy/downloadable/>
>>>>
>>>
>>>  
>>> http://pypi.python.org/pypi/**pygeoip/<http://pypi.python.org/pypi/pygeoip/>
>>> # pure Python
>>>>
>>>
>>> Thank you very much Tim.
>>> i am know trying to use it as:
>>>
>>> import pygeoip
>>>
>>> try:
>>>gic = pygeoip.GeoIP('/root/**GeoIPCity.dat')
>>>host = gic.time_zone_by_addr( os.environ['HTTP_CF_**CONNECTING_IP'] )
>>> except Exception as e:
>>>host = repr(e)
>>>
>>> lets hope it will work!
>>>
>>
>> Just my luck again,
>>
>> PermissionError(13, 'Άρνηση πρόσβασης')
>>
>> Άρνηση πρόσβασης = Access Denied
>>
>> Why would that happen?
>>
>
> root@nikos [~]# ls -l GeoLiteCity.dat
> -rw-r--r-- 1 root root 17633968 Jul  3 02:11 GeoLiteCity.dat
> root@nikos [~]# chmod +x GeoLiteCity.dat
> root@nikos [~]# ls -l GeoLiteCity.dat
> -rwxr-xr-x 1 root root 17633968 Jul  3 02:11 GeoLiteCity.dat*
> root@nikos [~]# python
> Python 3.3.2 (default, Jun  3 2013, 16:18:05)
> [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import pygeoip
>
> >>> gic = pygeoip.GeoIP('/root/**GeoIPCity.dat')
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/local/lib/python3.3/**site-packages/pygeoip-0.2.6-**
> py3.3.egg/pygeoip/__init__.py"**, line 110, in __init__
> self._filehandle = codecs.open(filename, 'rb', ENCODING)
>   File "/usr/local/lib/python3.3/**codecs.py", line 884, in open
> file = builtins.open(filename, mode, buffering)
>

Your code is not finding /root/GeoIPCity.dat because your directory has
this file: GeoLiteCity.dat

> FileNotFoundError: [Errno 2] No such file or directory:
> '/root/GeoIPCity.dat'


Aside from that you might have some permission problems since the file is
owned by root.  You should go back to old threads where this issue was
explained.

As was also pointed out, you only get information about where your isp is
located.  Phones and tablets find location from triangulating cell towers.
I don't think that laptops have that capability, and desktops probably even
less likely.

What is the purpose that you wish to serve.  I don't think you've thought
this through.

>
> >>>
>
>
>
> --
> What is now proved was at first only imagined!
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>



-- 
Joel Goldstick
http://joelgoldstick.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Νίκος Gr33k

Στις 6/7/2013 2:58 πμ, ο/η Νίκος Gr33k έγραψε:

Στις 6/7/2013 2:55 πμ, ο/η Νίκος Gr33k έγραψε:

Στις 5/7/2013 10:58 μμ, ο/η Tim Chase έγραψε:

On 2013-07-05 22:08, Νίκος Gr33k wrote:

Is there a way to extract out of some environmental variable the
Geo location of the user being the city the user visits out website
from?

Perhaps by utilizing his originated ip address?


Yep.  You can get an 11MB database (17MB uncompressed)

http://dev.maxmind.com/geoip/legacy/downloadable/



http://pypi.python.org/pypi/pygeoip/ # pure Python


Thank you very much Tim.
i am know trying to use it as:

import pygeoip

try:
   gic = pygeoip.GeoIP('/root/GeoIPCity.dat')
   host = gic.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
except Exception as e:
   host = repr(e)

lets hope it will work!


Just my luck again,

PermissionError(13, 'Άρνηση πρόσβασης')

Άρνηση πρόσβασης = Access Denied

Why would that happen?


root@nikos [~]# ls -l GeoLiteCity.dat
-rw-r--r-- 1 root root 17633968 Jul  3 02:11 GeoLiteCity.dat
root@nikos [~]# chmod +x GeoLiteCity.dat
root@nikos [~]# ls -l GeoLiteCity.dat
-rwxr-xr-x 1 root root 17633968 Jul  3 02:11 GeoLiteCity.dat*
root@nikos [~]# python
Python 3.3.2 (default, Jun  3 2013, 16:18:05)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygeoip
>>> gic = pygeoip.GeoIP('/root/GeoIPCity.dat')
Traceback (most recent call last):
  File "", line 1, in 
  File 
"/usr/local/lib/python3.3/site-packages/pygeoip-0.2.6-py3.3.egg/pygeoip/__init__.py", 
line 110, in __init__

self._filehandle = codecs.open(filename, 'rb', ENCODING)
  File "/usr/local/lib/python3.3/codecs.py", line 884, in open
file = builtins.open(filename, mode, buffering)
FileNotFoundError: [Errno 2] No such file or directory: 
'/root/GeoIPCity.dat'

>>>



--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Νίκος Gr33k

Στις 6/7/2013 2:55 πμ, ο/η Νίκος Gr33k έγραψε:

Στις 5/7/2013 10:58 μμ, ο/η Tim Chase έγραψε:

On 2013-07-05 22:08, Νίκος Gr33k wrote:

Is there a way to extract out of some environmental variable the
Geo location of the user being the city the user visits out website
from?

Perhaps by utilizing his originated ip address?


Yep.  You can get an 11MB database (17MB uncompressed)

http://dev.maxmind.com/geoip/legacy/downloadable/



http://pypi.python.org/pypi/pygeoip/ # pure Python


Thank you very much Tim.
i am know trying to use it as:

import pygeoip

try:
   gic = pygeoip.GeoIP('/root/GeoIPCity.dat')
   host = gic.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
except Exception as e:
   host = repr(e)

lets hope it will work!


Just my luck again,

PermissionError(13, 'Άρνηση πρόσβασης')

Άρνηση πρόσβασης = Access Denied

Why would that happen?


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 10:58 μμ, ο/η Tim Chase έγραψε:

On 2013-07-05 22:08, Νίκος Gr33k wrote:

Is there a way to extract out of some environmental variable the
Geo location of the user being the city the user visits out website
from?

Perhaps by utilizing his originated ip address?


Yep.  You can get an 11MB database (17MB uncompressed)

http://dev.maxmind.com/geoip/legacy/downloadable/



http://pypi.python.org/pypi/pygeoip/ # pure Python


Thank you very much Tim.
i am know tryitn to use it as:

import pygeoip

try:
  gic = pygeoip.GeoIP('/root/GeoIPCity.dat')
  host = gic.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
except Exception as e:
  host = repr(e)

lets hope it will work!


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Grant Edwards
On 2013-07-05, ?? Gr33k  wrote:

> Is there a way to extract out of some environmental variable the Geo 
> location of the user being the city the user visits out website from?

No.

> Perhaps by utilizing his originated ip address?

There is a very poor correlation between IP address and geographical
location.  I know that users of the ISPs in hometown are consistently
mis-identified as being from towns 1500km away.

-- 
Grant Edwards   grant.b.edwardsYow! If elected, Zippy
  at   pledges to each and every
  gmail.comAmerican a 55-year-old
   houseboy ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Tim Chase
On 2013-07-05 22:59, Support by Νίκος wrote:
> Στις 5/7/2013 10:58 μμ, ο/η Tim Chase έγραψε:
> > On 2013-07-05 22:08, Νίκος Gr33k wrote:
> >> Is there a way to extract out of some environmental variable the
> >> Geo location of the user being the city the user visits out
> >> website from?
> >>
> >> Perhaps by utilizing his originated ip address?
> > Yep.  You can get an 11MB database (17MB uncompressed)
> >
> > http://dev.maxmind.com/geoip/legacy/downloadable/
> >
> Thank you ill take a look

Alternatively, you[1] can use JavaScript in your code and have it
submit AJAX requests.  This interacts with the browser, asking it
where (geographically) it thinks it is.  This interacts with a GPS
some browsers (particularly mobile), while on others, it trusts the
user to report where they are.  Note that at any time, the user can
decline to answer or blatantly lie[2].

-tkc


[1]
generic "you"...this requires a fair bit of coding skill and the
ability to read/understand documentation that I can't say I've seen
demonstrated in certain recent threads.

[2]
https://addons.mozilla.org/en-us/firefox/addon/geolocater/








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


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Dave Angel

On 07/05/2013 04:44 PM, Tim Roberts wrote:

? Gr33k  wrote:


Is there a way to extract out of some environmental variable the Geo
location of the user being the city the user visits out website from?

Perhaps by utilizing his originated ip address?


It is possible to look up the geographic region associated with a block of
IP addresses.  That does not necessarily bear any resemblence to the actual
location of the user.  It tells you the location of the Internet provider
that registered the IP addresses.



To be more specific, the last time I checked, my IP address is 
registered to a location over 1000 miles away.  And it's not a dedicated 
IP address, so it could very well be used tomorrow by someone 200 nmiles 
the other side of the registered location.


Further, I sometimes use other ISP's with different addresses, even when 
I don't leave the house.  And once I leave, anything's possible.


The only things that travel with me and my machine are the cookies.  So 
if someone has asked me my location, and if I told the truth, then that 
information could remain available till I flush my cookies or change 
Virtual Machines or even browsers.


--
DaveA

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


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Tim Roberts
? Gr33k  wrote:
>
>Is there a way to extract out of some environmental variable the Geo 
>location of the user being the city the user visits out website from?
>
>Perhaps by utilizing his originated ip address?

It is possible to look up the geographic region associated with a block of
IP addresses.  That does not necessarily bear any resemblence to the actual
location of the user.  It tells you the location of the Internet provider
that registered the IP addresses.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Support by Νίκος

Στις 5/7/2013 10:58 μμ, ο/η Tim Chase έγραψε:

On 2013-07-05 22:08, Νίκος Gr33k wrote:

Is there a way to extract out of some environmental variable the
Geo location of the user being the city the user visits out website
from?

Perhaps by utilizing his originated ip address?

Yep.  You can get an 11MB database (17MB uncompressed)

http://dev.maxmind.com/geoip/legacy/downloadable/

which you can use to either populate an existing database with
the .CSV data there, or use the binary data blob in concert with the
Python API

https://github.com/maxmind/geoip-api-python  # Python + C
http://pypi.python.org/pypi/pygeoip/ # pure Python

Just be sure to adhere to the licensing terms.

-tkc






Thank you ill take a look on

http://pypi.python.org/pypi/pygeoip/  # pure Python

i hope it will be easy!


--
Webhost <http://superhost.gr>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Tim Chase
On 2013-07-05 22:08, Νίκος Gr33k wrote:
> Is there a way to extract out of some environmental variable the
> Geo location of the user being the city the user visits out website
> from?
> 
> Perhaps by utilizing his originated ip address?

Yep.  You can get an 11MB database (17MB uncompressed)

http://dev.maxmind.com/geoip/legacy/downloadable/

which you can use to either populate an existing database with
the .CSV data there, or use the binary data blob in concert with the
Python API

https://github.com/maxmind/geoip-api-python  # Python + C
http://pypi.python.org/pypi/pygeoip/ # pure Python

Just be sure to adhere to the licensing terms.

-tkc




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


Geo Location extracted from visitors ip address

2013-07-05 Thread Νίκος Gr33k
Is there a way to extract out of some environmental variable the Geo 
location of the user being the city the user visits out website from?


Perhaps by utilizing his originated ip address?

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: using text file to get ip address from hostname

2012-09-19 Thread Dan Katorza
בתאריך יום רביעי, 19 בספטמבר 2012 15:28:23 UTC+3, מאת Dan Katorza:
> בתאריך יום רביעי, 19 בספטמבר 2012 12:11:04 UTC+3, מאת Dan Katorza:
> 
> > בתאריך יום רביעי, 19 בספטמבר 2012 11:50:56 UTC+3, מאת Dan Katorza:
> 
> > 
> 
> > > בתאריך יום רביעי, 19 בספטמבר 2012 11:14:29 UTC+3, מאת Chris Angelico:
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > On Wed, Sep 19, 2012 at 5:41 PM, Dan Katorza  wrote:
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > >
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > > Hello again,
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > > I have another question and i hope you will understand me..
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > > Is there any option where you can set the program to go back to lets 
> > > > > say the top of the code?
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > > I mean if the program finished the operation and i want to stay in 
> > > > > the program and go back ro the start.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > > after any operation i want the option to do it again , go back to the 
> > > > > main menu or full exit from the program, and i want it every time.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > >
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > > i hope i'm clear :)
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > Yep! Look up the docs and tutorial on "control flow" and "looping
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > constructs". Sounds like what you want here is a 'while' loop.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > ChrisA
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > Hi Chris,
> 
> > 
> 
> > > 
> 
> > 
> 
> > > this is my code:
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > #!/usr/bin/env python
> 
> > 
> 
> > > 
> 
> > 
> 
> > > #Get the IP Address
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > import sys, socket
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > print ("\n\n#")
> 
> > 
> 
> > > 
> 
> > 
> 
> > > print ("#Get IP from Host v 1.0 #")
> 
> > 
> 
> > > 
> 
> > 
> 
> > > print ("#")
> 
> > 
> 
> > > 
> 
> > 
> 
> > > print ("# Choose from the options below #")
> 
> > 
> 
> > > 
> 
> > 
> 
> > > print ("#  1- url , 2-File(Text file only.txt)  #")
> 
> > 
> 
> > > 
> 
> > 
> 
> > > print ("#

Re: using text file to get ip address from hostname

2012-09-19 Thread Dave Angel
On 09/19/2012 08:28 AM, Dan Katorza wrote:
> בתאריך יום רביעי, 19 בספטמבר 2012 12:11:04 UTC+3, מאת Dan Katorza:
>> 
>> hi, ll like
>> found a solution,
>> it's not quite like Chris advised but it works.

Not at all like Chris advised.  But it also doesn't help you understand
programming.  Two concepts you're going to have to get a lot more
comfortable with, in Python, or in some other language.  One is loops,
and the other is functions.
>> #!/usr/bin/env python
>> #Get the IP Address
>>
>> import sys, socket, os
>>
>> def restart_program():
>> python = sys.executable
>> os.execl(python, python, * sys.argv)
>>
>> print ("\n\n#")
>> print ("#Get IP from Host v 1.0 #")
>> print ("#")
>> print ("# Choose from the options below #")
>> print ("#  1- url , 2-File(Text file only.txt)  #")
>> print ("#\n")
>>
>> mchoice = int(raw_input("Please enter your choice> "))
>> while mchoice !=1 and  mchoice !=2:
>> print("{0} is not a menu option.".format(mchoice))
>> mchoice = int(raw_input("Please try again> "))
>>
>>
>> while mchoice == 2:
>> filename = raw_input("Please enter file name here> ")
>> if filename.endswith(".txt"):
>>
>> try:
>> infile = open(filename)
>> except EnvironmentError as e:
>> print(e)
>> sys.exit(1)
>>
>> print("\nFile {0} exists!".format(filename))
>> print("\nGetting IP addresses for hosts")
>> print("\n")
>> else:
>> print("{0} is not a Text file.".format(filename))
>> sys.exit(1)
>> for line in infile:
>> hostname = line.strip()
>> try:
>> ip_address = socket.gethostbyname(hostname)
>> except EnvironmentError as e:
>> print("Couldn't find IP address for {0}: {1}".format(hostname, 
>> e))
>> continue
>> print("IP address for {0} is {1}.".format(hostname, ip_address))
>> else:
>> print ("\nFinished the operation")
>> print ("A=another search, M=main menu, E=exit")
>>
>> waction=raw_input("Please choose your action > ")
>>
>> while waction !='A' and waction !='M' and waction !='E':
>> print("{0} is not a valid action.".format(waction))
>> waction=raw_input("Please try again> ")
>> if waction =='E':
>> sys.exit(1)
>> if waction =='A':
>> continue
>> if waction =='M':
>> print 
>> ("#")
>> print ("# Choose from the options below 
>> #")
>> print ("#  1- url , 2-File(Text file only.txt)  
>> #")
>> print 
>> ("#\n")
>>
>> mchoice = int(raw_input("Please enter your choice> "))
>> while mchoice !=1 and  mchoice !=2:
>> print("{0} is not a menu option.".format(mchoice))
>> mchoice = int(raw_input("Please try again> "))
>>
>>
>> while mchoice == 1:
>> murl = raw_input("Enter URL here> ")
>> try:
>> print("Checking URL...")
>> ip_address = socket.gethostbyname(murl)
>> except EnvironmentError as d:
>> print(d)
>> sys.exit(1)
>> print("Valid URL")
>> print("\nIP address for {0} is {1}.".format(murl, ip_address))
>> print ("\nFinished the operation")
>> print ("A=another search, M=main menu, E=exit")
>>
>> waction=raw_input("Please choose your action > ")
>>
>> while waction !='A' and waction !='M' and waction !='E':
>> print("{0} is not a valid action.".format(waction))
>> waction=raw_input("Please try again> ")
>> if waction =='E':
>> sys.exit(1)
>> if waction =='A':
>> continue
>> if waction =='M':
>> restart_program()
>>
>>
>>
>>
This is one enormous top-level code, and when you needed to enclose it
in a loop, your answer is to start a new process!  You also duplicate
quite a few lines, rather than making a function for them, and calling
it from two places.


-- 

DaveA

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


Re: using text file to get ip address from hostname

2012-09-19 Thread Dan Katorza
בתאריך יום רביעי, 19 בספטמבר 2012 12:11:04 UTC+3, מאת Dan Katorza:
> בתאריך יום רביעי, 19 בספטמבר 2012 11:50:56 UTC+3, מאת Dan Katorza:
> 
> > בתאריך יום רביעי, 19 בספטמבר 2012 11:14:29 UTC+3, מאת Chris Angelico:
> 
> > 
> 
> > > On Wed, Sep 19, 2012 at 5:41 PM, Dan Katorza  wrote:
> 
> > 
> 
> > > 
> 
> > 
> 
> > > >
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > Hello again,
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > I have another question and i hope you will understand me..
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > Is there any option where you can set the program to go back to lets 
> > > > say the top of the code?
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > I mean if the program finished the operation and i want to stay in the 
> > > > program and go back ro the start.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > after any operation i want the option to do it again , go back to the 
> > > > main menu or full exit from the program, and i want it every time.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > >
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > i hope i'm clear :)
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > Yep! Look up the docs and tutorial on "control flow" and "looping
> 
> > 
> 
> > > 
> 
> > 
> 
> > > constructs". Sounds like what you want here is a 'while' loop.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > ChrisA
> 
> > 
> 
> > 
> 
> > 
> 
> > Hi Chris,
> 
> > 
> 
> > this is my code:
> 
> > 
> 
> > 
> 
> > 
> 
> > #!/usr/bin/env python
> 
> > 
> 
> > #Get the IP Address
> 
> > 
> 
> > 
> 
> > 
> 
> > import sys, socket
> 
> > 
> 
> > 
> 
> > 
> 
> > print ("\n\n#")
> 
> > 
> 
> > print ("#Get IP from Host v 1.0 #")
> 
> > 
> 
> > print ("#")
> 
> > 
> 
> > print ("# Choose from the options below #")
> 
> > 
> 
> > print ("#  1- url , 2-File(Text file only.txt)  #")
> 
> > 
> 
> > print ("#\n")
> 
> > 
> 
> > 
> 
> > 
> 
> > mchoice = int(raw_input("Please enter your choice> "))
> 
> > 
> 
> > while mchoice !=1 and  mchoice !=2:
> 
> > 
> 
> > print("{0} is not a menu option.".format(mchoice))
> 
> > 
> 
> > mchoice = int(raw_input("Please try again> "))
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > if mchoice == 2:
> 
> > 
> 
> >   filename = raw_input("Hello, please enter file name here> ")
> 
> > 
> 
> >   if filename.endswith(".txt"):
> 
> > 
> 
> > 
> 
> > 
> 
> >try:
> 
> > 
> 
> > infile = open(filename)
> 
> > 
> 
> >except EnvironmentError as e:
> 
> > 
> 
> > print(e)
> 
> > 
> 
> > sys.exit(1)
> 
> > 
> 
> > 
> 
> > 
> 
> >print("\nFile {0} exists!".format(filename))
> 
> > 
> 
> >print("\nGetting IP addresses for hosts")
> 
> > 
> 
> >print("\n")
> 
> > 
> 
> >   else:
> 
> > 
> 
> >print("{0} is not a Text file.".format(filename))
> 
> > 
> 
> >sys.exit(1)
> 
> > 
> 
> >   for line in infile:
> 
> > 
> 
> > hostname = line.strip()
> 
> > 
> 
> > try:
> 
> > 
> 
> > ip_address = socket.gethostbyname(hostname)
> 
> > 
> 
> > except EnvironmentError as e:
> 
> > 
> 
> > print("Couldn't find IP address for {0}: {1}".format(hostname, e))
> 
> &

Re: using text file to get ip address from hostname

2012-09-19 Thread Dan Katorza
בתאריך יום רביעי, 19 בספטמבר 2012 11:50:56 UTC+3, מאת Dan Katorza:
> בתאריך יום רביעי, 19 בספטמבר 2012 11:14:29 UTC+3, מאת Chris Angelico:
> 
> > On Wed, Sep 19, 2012 at 5:41 PM, Dan Katorza  wrote:
> 
> > 
> 
> > >
> 
> > 
> 
> > > Hello again,
> 
> > 
> 
> > > I have another question and i hope you will understand me..
> 
> > 
> 
> > > Is there any option where you can set the program to go back to lets say 
> > > the top of the code?
> 
> > 
> 
> > > I mean if the program finished the operation and i want to stay in the 
> > > program and go back ro the start.
> 
> > 
> 
> > > after any operation i want the option to do it again , go back to the 
> > > main menu or full exit from the program, and i want it every time.
> 
> > 
> 
> > >
> 
> > 
> 
> > > i hope i'm clear :)
> 
> > 
> 
> > 
> 
> > 
> 
> > Yep! Look up the docs and tutorial on "control flow" and "looping
> 
> > 
> 
> > constructs". Sounds like what you want here is a 'while' loop.
> 
> > 
> 
> > 
> 
> > 
> 
> > ChrisA
> 
> 
> 
> Hi Chris,
> 
> this is my code:
> 
> 
> 
> #!/usr/bin/env python
> 
> #Get the IP Address
> 
> 
> 
> import sys, socket
> 
> 
> 
> print ("\n\n#")
> 
> print ("#Get IP from Host v 1.0 #")
> 
> print ("#")
> 
> print ("# Choose from the options below #")
> 
> print ("#  1- url , 2-File(Text file only.txt)  #")
> 
> print ("#\n")
> 
> 
> 
> mchoice = int(raw_input("Please enter your choice> "))
> 
> while mchoice !=1 and  mchoice !=2:
> 
> print("{0} is not a menu option.".format(mchoice))
> 
> mchoice = int(raw_input("Please try again> "))
> 
> 
> 
> 
> 
> if mchoice == 2:
> 
>   filename = raw_input("Hello, please enter file name here> ")
> 
>   if filename.endswith(".txt"):
> 
> 
> 
>try:
> 
>     infile = open(filename)
> 
>except EnvironmentError as e:
> 
> print(e)
> 
> sys.exit(1)
> 
> 
> 
>print("\nFile {0} exists!".format(filename))
> 
>print("\nGetting IP addresses for hosts")
> 
>print("\n")
> 
>   else:
> 
>print("{0} is not a Text file.".format(filename))
> 
>sys.exit(1)
> 
>   for line in infile:
> 
> hostname = line.strip()
> 
> try:
> 
> ip_address = socket.gethostbyname(hostname)
> 
> except EnvironmentError as e:
> 
> print("Couldn't find IP address for {0}: {1}".format(hostname, e))
> 
> continue
> 
> print("IP address for {0} is {1}.".format(hostname, ip_address))
> 
>   else:
> 
> print ("\nFinished the operation")
> 
> 
> 
> if mchoice == 1:
> 
>   murl = raw_input("Enter URL here> ")
> 
>   try:
> 
>   print("Checking URL...")
> 
>   ip_address = socket.gethostbyname(murl)
> 
>   except EnvironmentError as d:
> 
>   print(d)
> 
>   sys.exit(1)
> 
>   print("Valid URL")
> 
>   print("\nIP address for {0} is {1}.".format(murl, ip_address))
> 
>   print ("\nFinished the operation")
> 
> =
> 
> 
> 
> now where it says Finsihed the operation i want it to show (another search 
> /main menu/exit program)
> 
> 
> 
> i know about the while loop , but forgive me i just don't have a clue how to 
> use it for this situation.
> 
> 
> 
> i don't want you to give me the code:) just the idea.
> 
> i did read the section about the while loop but still i do not know how to 
> use it in this situation.
> 
> thanks.

o.k a giant while loop :)
thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using text file to get ip address from hostname

2012-09-19 Thread Chris Angelico
On Wed, Sep 19, 2012 at 6:50 PM, Dan Katorza  wrote:
> i know about the while loop , but forgive me i just don't have a clue how to 
> use it for this situation.

You've already used one. What you need to do is surround your entire
code with the loop, so that as soon as it gets to the bottom, it goes
back to the top.

Tip: You'll be indenting the bulk of your code.

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


Re: using text file to get ip address from hostname

2012-09-19 Thread Dan Katorza
בתאריך יום רביעי, 19 בספטמבר 2012 11:14:29 UTC+3, מאת Chris Angelico:
> On Wed, Sep 19, 2012 at 5:41 PM, Dan Katorza  wrote:
> 
> >
> 
> > Hello again,
> 
> > I have another question and i hope you will understand me..
> 
> > Is there any option where you can set the program to go back to lets say 
> > the top of the code?
> 
> > I mean if the program finished the operation and i want to stay in the 
> > program and go back ro the start.
> 
> > after any operation i want the option to do it again , go back to the main 
> > menu or full exit from the program, and i want it every time.
> 
> >
> 
> > i hope i'm clear :)
> 
> 
> 
> Yep! Look up the docs and tutorial on "control flow" and "looping
> 
> constructs". Sounds like what you want here is a 'while' loop.
> 
> 
> 
> ChrisA

Hi Chris,
this is my code:

#!/usr/bin/env python
#Get the IP Address

import sys, socket

print ("\n\n#")
print ("#Get IP from Host v 1.0 #")
print ("#")
print ("# Choose from the options below #")
print ("#  1- url , 2-File(Text file only.txt)  #")
print ("#\n")

mchoice = int(raw_input("Please enter your choice> "))
while mchoice !=1 and  mchoice !=2:
print("{0} is not a menu option.".format(mchoice))
mchoice = int(raw_input("Please try again> "))


if mchoice == 2:
  filename = raw_input("Hello, please enter file name here> ")
  if filename.endswith(".txt"):

   try:
infile = open(filename)
   except EnvironmentError as e:
print(e)
sys.exit(1)

   print("\nFile {0} exists!".format(filename))
   print("\nGetting IP addresses for hosts")
   print("\n")
  else:
   print("{0} is not a Text file.".format(filename))
   sys.exit(1)
  for line in infile:
hostname = line.strip()
try:
ip_address = socket.gethostbyname(hostname)
except EnvironmentError as e:
print("Couldn't find IP address for {0}: {1}".format(hostname, e))
continue
print("IP address for {0} is {1}.".format(hostname, ip_address))
  else:
print ("\nFinished the operation")

if mchoice == 1:
  murl = raw_input("Enter URL here> ")
  try:
  print("Checking URL...")
  ip_address = socket.gethostbyname(murl)
  except EnvironmentError as d:
  print(d)
  sys.exit(1)
  print("Valid URL")
  print("\nIP address for {0} is {1}.".format(murl, ip_address))
  print ("\nFinished the operation")
=

now where it says Finsihed the operation i want it to show (another search 
/main menu/exit program)

i know about the while loop , but forgive me i just don't have a clue how to 
use it for this situation.

i don't want you to give me the code:) just the idea.
i did read the section about the while loop but still i do not know how to use 
it in this situation.
thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using text file to get ip address from hostname

2012-09-19 Thread Chris Angelico
On Wed, Sep 19, 2012 at 5:41 PM, Dan Katorza  wrote:
>
> Hello again,
> I have another question and i hope you will understand me..
> Is there any option where you can set the program to go back to lets say the 
> top of the code?
> I mean if the program finished the operation and i want to stay in the 
> program and go back ro the start.
> after any operation i want the option to do it again , go back to the main 
> menu or full exit from the program, and i want it every time.
>
> i hope i'm clear :)

Yep! Look up the docs and tutorial on "control flow" and "looping
constructs". Sounds like what you want here is a 'while' loop.

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


Re: using text file to get ip address from hostname

2012-09-19 Thread Dan Katorza
בתאריך יום ראשון, 16 בספטמבר 2012 01:43:31 UTC+3, מאת Dan Katorza:
> בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת Dan Katorza:
> 
> > hello ,
> 
> > 
> 
> > 
> 
> > 
> 
> > i'm new to Python and i searched the web and could not find an answer for 
> > my issue.
> 
> > 
> 
> > 
> 
> > 
> 
> > i need to get an ip address from list of hostnames which are in a textfile.
> 
> > 
> 
> > 
> 
> > 
> 
> > this is what i have so far 
> 
> > 
> 
> > --
> 
> > 
> 
> > #!/usr/bin/env python
> 
> > 
> 
> > #Get the IP Address
> 
> > 
> 
> > 
> 
> > 
> 
> > import socket
> 
> > 
> 
> > hostname = 'need it to read from a text file'
> 
> > 
> 
> > addr = socket.gethostbyname(hostname)
> 
> > 
> 
> > print 'The address of ', hostname, 'is', addr 
> 
> > 
> 
> > 
> 
> > 
> 
> > ---
> 
> > 
> 
> > 
> 
> > 
> 
> > any idea ? 
> 
> > 
> 
> > sorry for my english
> 
> > 
> 
> > 
> 
> > 
> 
> > thanks.
> 
> 
> 
> Hi Hans,
> 
> thank you very much for the tips.
> 
> as i mentioned before I'm new to python and I'm trying to learn it step by 
> step.
> 
> thank you for showing me other ways, i will explore them.

Hello again,
I have another question and i hope you will understand me..
Is there any option where you can set the program to go back to lets say the 
top of the code?
I mean if the program finished the operation and i want to stay in the program 
and go back ro the start.
after any operation i want the option to do it again , go back to the main menu 
or full exit from the program, and i want it every time.

i hope i'm clear :)


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


Re: using text file to get ip address from hostname

2012-09-17 Thread Thomas Rachel

Am 15.09.2012 18:20 schrieb Dan Katorza:


hello again friends,
thanks for everyone help on this.
i guess i figured it out in two ways.
the second one i prefer the most.

i will appreciate if someone can give me some tips.
thanks again

so...
-
First
-
#!/usr/bin/env python
#Get the IP Address


print("hello, please enter file name here>"),
import socket
for line in open(raw_input()):
 hostname = line.strip()
     print("IP address for {0} is 
{1}.".format(hostname,socket.gethostbyname(hostname)))


second

#!/usr/bin/env python
#Get the IP Address

import os

print("Hello, please enter file name here>"),
FILENAME = raw_input()
if os.path.isfile(FILENAME):
 print("\nFile Exist!")
 print("\nGetting ip from host name")
 print("\n")
 import socket
 for line in open (FILENAME):
 hostname = line.strip()
 print("IP address for {0} is 
{1}.".format(hostname,socket.gethostbyname(hostname)))
 else:
 print ("\nFinished the operation")
else:
 print ("\nFIle is missing or is not reasable"),
~


Comparing these, the first one wins if you catch and process exceptions. 
It is easier to ask for forgiveness than to get permission (EAFP, 
http://en.wikipedia.org/wiki/EAFP).


Bit I wonder that no one has mentionned that 
socket.gethostbyname(hostname) is quite old-age because it only returns 
IPv4 addresses (resp. only one of them).


OTOH, socket.getaddrinfo(hostname, 0, 0, socket.SOCK_STREAM) gives you a 
list of parameter tuples for connecting.


So which way you go above, you should change the respective lines to

for line in ...:
hostname = line.strip()
for target in socket.getaddrinfo(hostname, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM):
print("IP address for {0} is {1}.".format(hostname,
target[4][0]))


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


Re: using text file to get ip address from hostname

2012-09-15 Thread Dan Katorza
בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת Dan Katorza:
> hello ,
> 
> 
> 
> i'm new to Python and i searched the web and could not find an answer for my 
> issue.
> 
> 
> 
> i need to get an ip address from list of hostnames which are in a textfile.
> 
> 
> 
> this is what i have so far 
> 
> ----------
> 
> #!/usr/bin/env python
> 
> #Get the IP Address
> 
> 
> 
> import socket
> 
> hostname = 'need it to read from a text file'
> 
> addr = socket.gethostbyname(hostname)
> 
> print 'The address of ', hostname, 'is', addr 
> 
> 
> 
> ---
> 
> 
> 
> any idea ? 
> 
> sorry for my english
> 
> 
> 
> thanks.

Hi Hans,
thank you very much for the tips.
as i mentioned before I'm new to python and I'm trying to learn it step by step.
thank you for showing me other ways, i will explore them.

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


Re: using text file to get ip address from hostname

2012-09-15 Thread Hans Mulder
On 15/09/12 18:20:42, Dan Katorza wrote:
> בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת Dan Katorza:
>> hello ,
>>
>>
>>
>> i'm new to Python and i searched the web and could not find an answer for my 
>> issue.
>>
>>
>>
>> i need to get an ip address from list of hostnames which are in a textfile.
>>
>>
>>
>> this is what i have so far 
>>
>> ----------
>>
>> #!/usr/bin/env python
>>
>> #Get the IP Address
>>
>>
>>
>> import socket
>>
>> hostname = 'need it to read from a text file'
>>
>> addr = socket.gethostbyname(hostname)
>>
>> print 'The address of ', hostname, 'is', addr 
>>
>>
>>
>> ---
>>
>>
>>
>> any idea ? 
>>
>> sorry for my english
>>
>>
>>
>> thanks.
> 
> hello again friends,
> thanks for everyone help on this.
> i guess i figured it out in two ways.
> the second one i prefer the most.
> 
> i will appreciate if someone can give me some tips.
> thanks again 
> 
> so...
> -
> First 
> -
> #!/usr/bin/env python
> #Get the IP Address
> 
> 
> print("hello, please enter file name here >"),

Instead of printing this string, you can pass it as the
argument to raw_input:

for line in open(raw_input("hello, please enter file name here> ")):

Cosmetically, I'd prefer a space after the '>'.

> import socket

PEP 8 recommends putting all imports at the top of the file, like you
do in your second script.

> for line in open(raw_input()):
> hostname = line.strip()
> print("IP address for {0} is 
> {1}.".format(hostname,socket.gethostbyname(hostname)))

To my mind, this line does two things: it finds the IP address and
prints it.  I think it would be more readable to do these on separate
lines:

ip_address = socket.gethostbyname(hostname)
print("IP address for {0} is {1}.".format(hostname, ip_address)

This forces you to find a good name for the value returned
by gethostbyname, which I consider a Good Thing (tm).

PEP 8 recommends that lines should not be longer than 80
characters.  Your line is longer than that; splitting it
in two conceptual steps nicely solves that issue as well.

> 
> second
> 
> #!/usr/bin/env python
> #Get the IP Address
> 
> import os
> 
> print("Hello, please enter file name here >"),
> FILENAME = raw_input()

PEP 8 recommends all upper case for constants, for example
socket.IPPROTO_IPV6.  The filename here is not a hard-wired
constant, so it should be in lower case.

> if os.path.isfile(FILENAME):

Your first script allowed me to enter "/dev/tty" at the prompt,
and then type a bunch of hostnames and an end-of-file character.
This script reports "FIle is missing or not reasable", because
/dev/tty is not a regular file.  I think the message should be
"File is missing or not readable or not a regular file".

I'm always annoyed when I get an error message with several
"or"s in it.  I prefer programs that figure out which of the
three potential issues is the case, and mention only one cause.

> print("\nFile Exist!")
> print("\nGetting ip from host name")
> print("\n")
> import socket
> for line in open (FILENAME):
> hostname = line.strip()
> print("IP address for {0} is 
> {1}.".format(hostname,socket.gethostbyname(hostname)))
> else:
> print ("\nFinished the operation")
> else:
> print ("\nFIle is missing or is not reasable"),

You don't want a comma at the end of this line: it messes
up the next shell prompt.

Also, this line a rather far away from the test that triggers it.

How about:

filename = raw_input("Hello, please enter file name here> ")
if not os.path.isfile(filename):
if not os.exist(filename):
print("\nFile {} does not exist")
else:
print("\nFile {} is not a regular file")
sys.exit(1)

print("\nFile {} exists", filename)
# etc.

Or you could skip the whole 'os' thing and use a try/except
construct instead:

#!/usr/bin/env python
#Get the IP Address

import sys, socket

filename = raw_input("Hello, please enter file name here>

Re: using text file to get ip address from hostname

2012-09-15 Thread Dan Katorza
בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת Dan Katorza:
> hello ,
> 
> 
> 
> i'm new to Python and i searched the web and could not find an answer for my 
> issue.
> 
> 
> 
> i need to get an ip address from list of hostnames which are in a textfile.
> 
> 
> 
> this is what i have so far 
> 
> ----------
> 
> #!/usr/bin/env python
> 
> #Get the IP Address
> 
> 
> 
> import socket
> 
> hostname = 'need it to read from a text file'
> 
> addr = socket.gethostbyname(hostname)
> 
> print 'The address of ', hostname, 'is', addr 
> 
> 
> 
> ---
> 
> 
> 
> any idea ? 
> 
> sorry for my english
> 
> 
> 
> thanks.

hello again friends,
thanks for everyone help on this.
i guess i figured it out in two ways.
the second one i prefer the most.

i will appreciate if someone can give me some tips.
thanks again 

so...
-
First 
-
#!/usr/bin/env python
#Get the IP Address


print("hello, please enter file name here >"),
import socket
for line in open(raw_input()):
hostname = line.strip()
print("IP address for {0} is 
{1}.".format(hostname,socket.gethostbyname(hostname)))


second

#!/usr/bin/env python
#Get the IP Address

import os

print("Hello, please enter file name here >"),
FILENAME = raw_input()
if os.path.isfile(FILENAME):
print("\nFile Exist!")
print("\nGetting ip from host name")
print("\n")
import socket
for line in open (FILENAME):
hostname = line.strip()
print("IP address for {0} is 
{1}.".format(hostname,socket.gethostbyname(hostname)))
else:
print ("\nFinished the operation")
else:
print ("\nFIle is missing or is not reasable"),
~   
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using text file to get ip address from hostname

2012-09-12 Thread Jason Friedman
> i need to get an ip address from list of hostnames which are in a textfile.
>
> this is what i have so far
> --
> #!/usr/bin/env python
> #Get the IP Address
>
> import socket
> hostname = 'need it to read from a text file'
> addr = socket.gethostbyname(hostname)
> print 'The address of ', hostname, 'is', addr

$ cat hostnames
google.com
microsoft.com
facebook.com

$ python3
Python 3.2.3 (default, May  3 2012, 15:51:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> for line in open("hostnames"):
... hostname = line.strip()
... print("IP address for {0} is {1}.".format(hostname,
socket.gethostbyname(hostname)))
...
IP address for google.com is 74.125.225.33.
IP address for microsoft.com is 64.4.11.37.
IP address for facebook.com is 69.171.237.16.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using text file to get ip address from hostname

2012-09-12 Thread Terry Reedy

On 9/12/2012 10:41 AM, dkato...@gmail.com wrote:



it's not really homework, i found a lab exercise on the web

> and i;m trying to study with it. maybe not the most efficient way.


i have a file with hostnames ordered line by line.


Key fact for this exercise: open files are iterable.
So your top level structure is...

for line in open():
  

--
Terry Jan Reedy

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


Re: using text file to get ip address from hostname

2012-09-12 Thread Alister
On Wed, 12 Sep 2012 07:41:10 -0700, dkatorza wrote:

> בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת dkat...@gmail.com:
>> hello ,
>> 
>> 
>> 
>> i'm new to Python and i searched the web and could not find an answer
>> for my issue.
>> 
>> 
>> 
>> i need to get an ip address from list of hostnames which are in a
>> textfile.
>> 
>> 
>> 
>> this is what i have so far
>> 
>> 
----------
>> 
>> #!/usr/bin/env python
>> 
>> #Get the IP Address
>> 
>> 
>> 
>> import socket
>> 
>> hostname = 'need it to read from a text file'
>> 
>> addr = socket.gethostbyname(hostname)
>> 
>> print 'The address of ', hostname, 'is', addr
>> 
>> 
>> 
>> 
---
>> 
>> 
>> 
>> any idea ?
>> 
>> sorry for my english
>> 
>> 
>> 
>> thanks.
> 
> thank you ChrisA it's not really homework, i found a lab exercise on the
> web and i;m trying to study with it. maybe not the most efficient way.
> 
> i have a file with hostnames ordered line by line.
> 
> i will check the sources and will get back with outcome.
> 
> once again , Thanks,

so self inflicted homework :-), a good way to learn so ChrisA's pointers 
of where to look are probably the best thing for you.

if you get stuck post back sample code & i am sure someone will be happy 
to hint a little deeper

Good luck



-- 
QOTD:
"Wouldn't it be wonderful if real life supported control-Z?"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using text file to get ip address from hostname

2012-09-12 Thread dkatorza
בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת dkat...@gmail.com:
> hello ,
> 
> 
> 
> i'm new to Python and i searched the web and could not find an answer for my 
> issue.
> 
> 
> 
> i need to get an ip address from list of hostnames which are in a textfile.
> 
> 
> 
> this is what i have so far 
> 
> ----------
> 
> #!/usr/bin/env python
> 
> #Get the IP Address
> 
> 
> 
> import socket
> 
> hostname = 'need it to read from a text file'
> 
> addr = socket.gethostbyname(hostname)
> 
> print 'The address of ', hostname, 'is', addr 
> 
> 
> 
> ---
> 
> 
> 
> any idea ? 
> 
> sorry for my english
> 
> 
> 
> thanks.

thank you ChrisA
it's not really homework, i found a lab exercise on the web and i;m trying to 
study with it. maybe not the most efficient way.

i have a file with hostnames ordered line by line.

i will check the sources and will get back with outcome.

once again , Thanks,
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using text file to get ip address from hostname

2012-09-12 Thread Chris Angelico
On Thu, Sep 13, 2012 at 12:24 AM,   wrote:
> i'm new to Python and i searched the web and could not find an answer for my 
> issue.
>
> i need to get an ip address from list of hostnames which are in a textfile.

This is sounding like homework, so I'll just give you a basic pointer.

You have there something that successfully resolves one hostname to an
IP address. Now you want to expand that to reading an entire file of
them and resolving them all. Presumably you need to produce a list of
IP addresses; check the question as to whether you need to create a
file, or output to the screen, or something else.

What you want, here, is to open a file and iterate over it. The most
convenient way would be to have one hostname per line and iterate over
the lines of the file. Check out these pages in the Python docs
(you're using Python 2 so I'm going with Python 2.7.3 documentation):

Opening a file:
http://docs.python.org/library/functions.html#open
Ensuring that it'll be closed when you're done:
http://docs.python.org/reference/compound_stmts.html#the-with-statement
Looping over an iterable:
http://docs.python.org/tutorial/controlflow.html#for-statements

See where that takes you; in fact, all of
http://docs.python.org/tutorial/ is worth reading, if you haven't
already.

Have fun, enjoy Python!

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


using text file to get ip address from hostname

2012-09-12 Thread dkatorza
hello ,

i'm new to Python and i searched the web and could not find an answer for my 
issue.

i need to get an ip address from list of hostnames which are in a textfile.

this is what i have so far 
--
#!/usr/bin/env python
#Get the IP Address

import socket
hostname = 'need it to read from a text file'
addr = socket.gethostbyname(hostname)
print 'The address of ', hostname, 'is', addr 

---

any idea ? 
sorry for my english

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


Re: Get the IP address of WIFI interface

2011-05-16 Thread Jun Hu
Thanks, this code works perfectly in ubuntu 10.04.
one question though, is dbus usually implemented in other distribution of
linux?

On Mon, May 16, 2011 at 12:57 PM, Anssi Saari  wrote:

> Neal Becker  writes:
>
> One possible solution in Linux is asking NetworkManager, if it's in
> use. It knows which interfaces are active and what kind they are (LAN,
> WLAN, WWAN etc.) NetworkManager communicates via dbus and even
> includes python example scripts. So here's my scriptlet based on
> NetworkManager example nm-state.py. This one prints out all active
> devices and their type and IP address. Easily modified to print only
> WLAN types.
>
> import dbus, socket, struct
>
> bus = dbus.SystemBus()
>
> proxy = bus.get_object("org.freedesktop.NetworkManager",
> "/org/freedesktop/NetworkManager")
> manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
>
> # Get device-specific state
> devices = manager.GetDevices()
> for d in devices:
>dev_proxy = bus.get_object("org.freedesktop.NetworkManager", d)
>prop_iface = dbus.Interface(dev_proxy,
> "org.freedesktop.DBus.Properties")
>
># Get the device's current state and interface name
>state = prop_iface.Get("org.freedesktop.NetworkManager.Device", "State")
>name = prop_iface.Get("org.freedesktop.NetworkManager.Device",
> "Interface")
>ifa = "org.freedesktop.NetworkManager.Device"
>type = prop_iface.Get(ifa, "DeviceType")
>addr = prop_iface.Get(ifa, "Ip4Address")
>
># and print them out
>if state == 8:   # activated
>addr_dotted = socket.inet_ntoa(struct.pack('
>s = "Device %s is activated and has type %s and address %s"
>print s % (name, type, addr_dotted)
>else:
>print "Device %s is not activated" % name
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get the IP address of WIFI interface

2011-05-16 Thread Anssi Saari
Neal Becker  writes:

> Here's some useful snippits for linux:
>
> def get_default_if():
> f = open('/proc/net/route')
> for i in csv.DictReader(f, delimiter="\t"):
> if long(i['Destination'], 16) == 0:
> return i['Iface']
> return None
>
> 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])

One possible solution in Linux is asking NetworkManager, if it's in
use. It knows which interfaces are active and what kind they are (LAN,
WLAN, WWAN etc.) NetworkManager communicates via dbus and even
includes python example scripts. So here's my scriptlet based on
NetworkManager example nm-state.py. This one prints out all active
devices and their type and IP address. Easily modified to print only
WLAN types.

import dbus, socket, struct

bus = dbus.SystemBus()

proxy = bus.get_object("org.freedesktop.NetworkManager", 
"/org/freedesktop/NetworkManager")
manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")

# Get device-specific state
devices = manager.GetDevices()
for d in devices:
dev_proxy = bus.get_object("org.freedesktop.NetworkManager", d)
prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties")

# Get the device's current state and interface name
state = prop_iface.Get("org.freedesktop.NetworkManager.Device", "State")
name = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface")
ifa = "org.freedesktop.NetworkManager.Device"
type = prop_iface.Get(ifa, "DeviceType")
addr = prop_iface.Get(ifa, "Ip4Address")

# and print them out
if state == 8:   # activated
addr_dotted = socket.inet_ntoa(struct.pack('http://mail.python.org/mailman/listinfo/python-list


Re: Get the IP address of WIFI interface

2011-05-15 Thread MrJean1
Perhaps, this recipe works for your case:

  

It does parse ifconfig and ipconfig, if found.

/Jean



On May 15, 2:14 pm, Andrew Berg  wrote:
> On 2011.05.15 06:12 AM, Tim Golden wrote:> ... and for Windows:
>
> > 
> > import wmi
>
> > for nic in wmi.WMI ().Win32_NetworkAdapterConfiguration (IPEnabled=1):
> >    print nic.Caption, nic.IPAddress
>
> > 
>
> One thing I found out about Win32_NetworkAdapterConfiguration is that it
> only contains /current/ information and not the stored info that it uses
> when making an initial connection (you can see and edit this info in the
> Network and Sharing Center applet). The difference is that if you're
> offline, that WMI object will have no useful info at all. You can find
> the info in the registry if you know what the UUID (or whatever it is)
> of (or assigned to) the interface (it's in
> HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Inter 
> faces).
> The OP said the card would be connected, so it might not be an issue,
> but I think it's important to know that. Wouldn't want you to suddenly
> get blank strings or exceptions and not know why. ;-)

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


Re: Get the IP address of WIFI interface

2011-05-15 Thread Jun Hu
On Sun, May 15, 2011 at 2:14 PM, Andrew Berg wrote:

>
> One thing I found out about Win32_NetworkAdapterConfiguration is that it
> only contains /current/ information and not the stored info that it uses
> when making an initial connection (you can see and edit this info in the
> Network and Sharing Center applet). The difference is that if you're
> offline, that WMI object will have no useful info at all. You can find
> the info in the registry if you know what the UUID (or whatever it is)
> of (or assigned to) the interface (it's in
>
> HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces).
> The OP said the card would be connected, so it might not be an issue,
> but I think it's important to know that. Wouldn't want you to suddenly
> get blank strings or exceptions and not know why. ;-)
>
> Thanks for the reminder, however, it seems the IPAddress
of Win32_NetworkAdapterConfiguration will be 0.0.0.0 if the interface is NOT
connected (at least that is the result on my winxp), so I think we are safe
here.   ^_^
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get the IP address of WIFI interface

2011-05-15 Thread Andrew Berg
On 2011.05.15 06:12 AM, Tim Golden wrote:
> ... and for Windows:
>
> 
> import wmi
>
> for nic in wmi.WMI ().Win32_NetworkAdapterConfiguration (IPEnabled=1):
>print nic.Caption, nic.IPAddress
>
> 
One thing I found out about Win32_NetworkAdapterConfiguration is that it
only contains /current/ information and not the stored info that it uses
when making an initial connection (you can see and edit this info in the
Network and Sharing Center applet). The difference is that if you're
offline, that WMI object will have no useful info at all. You can find
the info in the registry if you know what the UUID (or whatever it is)
of (or assigned to) the interface (it's in
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces).
The OP said the card would be connected, so it might not be an issue,
but I think it's important to know that. Wouldn't want you to suddenly
get blank strings or exceptions and not know why. ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get the IP address of WIFI interface

2011-05-15 Thread Tim Golden

On 15/05/2011 20:23, Jun Hu wrote:

Thanks for the tip, it is really helpful!
however the class of Win32_NetworkAdapterConfiguration doesn't include
the interface type (you can NOT tell if it is a wifi interface), so I
change the code a bit like following:

import wmi

wlan_int_id=None
for nic in wmi.WMI().Win32_NetworkAdapter():
 if nic.NetConnectionID == "Wireless Network Connection":
 wlan_int_id=nic.Index
 break

if wlan_int_id<>None:
 for nic in wmi.WMI ().Win32_NetworkAdapterConfiguration (IPEnabled=1):
 if nic.Index==wlan_int_id:
 print nic.IPAddress[0]
else:
 print "WLAN interface NOT Found"


Glad it was useful; you can get a little bit prettier:


import wmi

c = wmi.WMI ()
for nic in c.Win32_NetworkAdapter (
  NetConnectionID="Wireless Network Connection"
):
  for config in nic.associators (
wmi_result_class="Win32_NetworkAdapterConfiguration"
  ):
print config.Caption, "=>", " / ".join (config.IPAddress)
  break
else:
  print "No Wireless NIC found"




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


Re: Get the IP address of WIFI interface

2011-05-15 Thread Jun Hu
Thanks for the tip, it is really helpful!
however the class of Win32_NetworkAdapterConfiguration doesn't include the
interface type (you can NOT tell if it is a wifi interface), so I change the
code a bit like following:

import wmi

wlan_int_id=None
for nic in wmi.WMI().Win32_NetworkAdapter():
if nic.NetConnectionID == "Wireless Network Connection":
wlan_int_id=nic.Index
break

if wlan_int_id<>None:
for nic in wmi.WMI ().Win32_NetworkAdapterConfiguration (IPEnabled=1):
if nic.Index==wlan_int_id:
print nic.IPAddress[0]
else:
print "WLAN interface NOT Found"



On Sun, May 15, 2011 at 4:12 AM, Tim Golden  wrote:

> On 15/05/2011 12:04 PM, Neal Becker wrote:
>
>> Far.Runner wrote:
>>
>>  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.
>>> The question is: How to get the ip address of the wifi interface in a
>>> python
>>> script without parsing the output of a shell command like "ipconfig" or
>>> "ifconfig"?
>>>
>>> OS: Windows or Linux
>>>
>>> F.R
>>>
>>
>> Here's some useful snippits for linux:
>>
>
> ... and for Windows:
>
> 
> import wmi
>
> for nic in wmi.WMI ().Win32_NetworkAdapterConfiguration (IPEnabled=1):
>  print nic.Caption, nic.IPAddress
>
> 
>
> TJG
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get the IP address of WIFI interface

2011-05-15 Thread Tim Golden

On 15/05/2011 12:04 PM, Neal Becker wrote:

Far.Runner wrote:


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.
The question is: How to get the ip address of the wifi interface in a python
script without parsing the output of a shell command like "ipconfig" or
"ifconfig"?

OS: Windows or Linux

F.R


Here's some useful snippits for linux:


... and for Windows:


import wmi

for nic in wmi.WMI ().Win32_NetworkAdapterConfiguration (IPEnabled=1):
  print nic.Caption, nic.IPAddress



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


Re: Get the IP address of WIFI interface

2011-05-15 Thread Neal Becker
Far.Runner wrote:

> 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.
> The question is: How to get the ip address of the wifi interface in a python
> script without parsing the output of a shell command like "ipconfig" or
> "ifconfig"?
> 
> OS: Windows or Linux
> 
> F.R

Here's some useful snippits for linux:

def get_default_if():
f = open('/proc/net/route')
for i in csv.DictReader(f, delimiter="\t"):
if long(i['Destination'], 16) == 0:
return i['Iface']
return None

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])


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


Re: Get IP address of WIFI interface

2011-05-14 Thread Jun Hu
Thanks, is there any other way without using external command?

On Fri, May 13, 2011 at 10:41 PM, Ishwor Gurung wrote:

> Hi.
>
> On 14 May 2011 14:46, Far.Runner  wrote:
> > 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
>
> Detect the OS with os.name and branch out to specific use case.
>
> The specific functionality can be implemented 2 ways:
> 1/ Regular expression pattern match
> 2/ Substring match and splits
>
> The subprocess module will then let you run those commands.
> 1/ posix - (Linux in your case) will use ifconfig
> 2/ nt - (windows in your ase) will use ipconfig.
>
> HTH.
>
>
>
> --
>
> Regards
> Ishwor Gurung
> Key id:0xa98db35e
> Key fingerprint:FBEF 0D69 6DE1 C72B A5A8  35FE 5A9B F3BB 4E5E 17B5
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get IP address of WIFI interface

2011-05-13 Thread Ishwor Gurung
Hi.

On 14 May 2011 14:46, Far.Runner  wrote:
> 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

Detect the OS with os.name and branch out to specific use case.

The specific functionality can be implemented 2 ways:
1/ Regular expression pattern match
2/ Substring match and splits

The subprocess module will then let you run those commands.
1/ posix - (Linux in your case) will use ifconfig
2/ nt - (windows in your ase) will use ipconfig.

HTH.



-- 

Regards
Ishwor Gurung
Key id:0xa98db35e
Key fingerprint:FBEF 0D69 6DE1 C72B A5A8  35FE 5A9B F3BB 4E5E 17B5
-- 
http://mail.python.org/mailman/listinfo/python-list


How to get the IP address of the wifi interface?

2011-05-13 Thread Jun Hu
Hi python experts:
There are two network interfaces on my laptop, one is 100M Ethernet, the
other is wifi, both are connected and have IP addresses.
The question is: how to get the ip address of WIFI interface without parsing
the output of a shell command like "ipconfig" or "ifconfig"?

OS: Windows or Linux

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


Get the 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.
The question is: How to get the ip address of the wifi interface in a python
script without parsing the output of a shell command like "ipconfig" or
"ifconfig"?

OS: Windows or Linux

F.R
-- 
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: Cross-platform way to retrieve the current (Operative system) DNS server IP address in python

2010-04-30 Thread Hans Mulder

joamag wrote:


It's not my ip address that I want to discover... I want to discover
my default dns server ip address.
This ip is stored in an operative system basis.

Dos anyone know how to get it ?


You asked for a cross-platform solution; there probably isn't one.

The code below works on Unix and MacOS X.
If you can find code that works on Windows, you can cobble together
somthing that will work on most platforms.

dns_ips = []

for line in file('/etc/resolv.conf', 'r'):
columns = line.split()
if columns[0] == 'nameserver':
dns_ips.extend(columns[1:])

print dns_ips


Hope this help,

-- HansM

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


Re: Cross-platform way to retrieve the current (Operative system) DNS server IP address in python

2010-04-27 Thread Tiago Katcipis
maybe this helps you:

http://pypi.python.org/pypi/netifaces/0.3

best regards,
Katcipis

On Tue, Apr 27, 2010 at 7:49 PM, joamag  wrote:

> On 24 Abr, 14:50, DarkBlue  wrote:
> > On Apr 22, 4:55 pm, joamag  wrote:
> >
> > > Does anybody know a cross platform way to retrieve the default DNS
> > > server IP address in python ?
> >
> > > Thanks !
> > > João
> >
> > import os,urllib2,re
> >
> > def getIpAddr():
> > """
> > Function for parsing external ip adress by pinging dyndns.com
> > """
> > 
> > External_IP=urllib2.urlopen('http://checkip.dyndns.com/').read(<http://checkip.dyndns.com/%27%29.read%28>
> )
> > m = re.search(r"(([0-9]+\.){3}[0-9]+)", External_IP)
> > my_IP= m.group(1)
> > return my_IP
> >
> > print('Current Ip from DynDns :  %s ') % getIpAddr()
> >
> > this gets you your ip address
> >
> > hope it helps.
>
> Hi,
>
> It's not my ip address that I want to discover... I want to discover
> my default dns server ip address.
> This ip is stored in an operative system basis.
>
> Dos anyone know how to get it ?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cross-platform way to retrieve the current (Operative system) DNS server IP address in python

2010-04-27 Thread joamag
On 24 Abr, 14:50, DarkBlue  wrote:
> On Apr 22, 4:55 pm, joamag  wrote:
>
> > Does anybody know a cross platform way to retrieve the default DNS
> > server IP address in python ?
>
> > Thanks !
> > João
>
> import os,urllib2,re
>
> def getIpAddr():
>     """
>     Function for parsing external ip adress by pinging dyndns.com
>     """
>     External_IP=urllib2.urlopen('http://checkip.dyndns.com/').read()
>     m = re.search(r"(([0-9]+\.){3}[0-9]+)", External_IP)
>     my_IP= m.group(1)
>     return my_IP
>
> print('Current Ip from DynDns :  %s ') % getIpAddr()
>
> this gets you your ip address
>
> hope it helps.

Hi,

It's not my ip address that I want to discover... I want to discover
my default dns server ip address.
This ip is stored in an operative system basis.

Dos anyone know how to get it ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cross-platform way to retrieve the current (Operative system) DNS server IP address in python

2010-04-24 Thread DarkBlue
On Apr 22, 4:55 pm, joamag  wrote:
> Does anybody know a cross platform way to retrieve the default DNS
> server IP address in python ?
>
> Thanks !
> João


import os,urllib2,re


def getIpAddr():
"""
Function for parsing external ip adress by pinging dyndns.com
"""
External_IP=urllib2.urlopen('http://checkip.dyndns.com/').read()
m = re.search(r"(([0-9]+\.){3}[0-9]+)", External_IP)
my_IP= m.group(1)
return my_IP




print('Current Ip from DynDns :  %s ') % getIpAddr()



this gets you your ip address

hope it helps.
-- 
http://mail.python.org/mailman/listinfo/python-list


Cross-platform way to retrieve the current (Operative system) DNS server IP address in python

2010-04-22 Thread joamag
Does anybody know a cross platform way to retrieve the default DNS
server IP address in python ?

Thanks !
João
-- 
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


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


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: 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


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-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-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 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 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-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-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 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


  1   2   3   >