Question:
zip code based on ip address?
KoR
2011-01-03 09:35:36 UTC
I'm writing a bash script to tell me the weather in my current location. The idea is to use my external IP address to determine my location, and then get the weather. My problem is that I can't find any websites or services that will tell me the zip code of my location, (in such a way that I can run it in the terminal).
My script works, except that I need to turn an ip address into a US zip code. Any ideas?
Three answers:
michael
2011-01-03 10:06:09 UTC
Try using a Geolocation API. It's a little bit difficult to write "web apps" in bash; you would have to use curl or wget. You might have better luck with python, ruby or perl (etc).



Edit: Sorry, I guess I wasn't that clear... By "web app", I actually meant client-side web app: one that uses http requests / web services. But you're right, people usually mean server-side app's when they say "web app" (that's changing though... a lot). So, that's why I suggested a geolocation api (google it... there are many) and a tool like wget or curl. Bash doesn't have any networking functions built-in per se. But unix/linux/mac's all can run millions of utilities... (as can windows, via cygwin).



Anyway, for example, using hostip.info (just one example), let's say your IP address is 148.87.67.135 :



$ wget -nv -O - 'http://api.hostip.info/get_html.php?ip=148.87.67.135&position=true'

Country: UNITED STATES (US)

City: Reston, VA



Latitude: 38.95

Longitude: -77.35

IP: 148.87.67.135



** note: this forum hides the url in the command... it should still work if you hold your mouse over it and look in the "status bar" area of your browser. Here's the format of the url: http + api.hostip.info + get_html.php ? ip={your_IP_address} & optional_arg={some_value}'



You can leave off the IP, and it'll use your current IP. The "position" (lat/long) is also optional. Note that it's common that you're behind a firewall and on a local network (e.g., a 192.* or 10.* network), which has a useless / meaningless IP address for this purpose, so it may be better to let the server look up your IP. But the actual IP the server sees may be from your ISP, which is possibly not as "local" as you would like. (Details, details...)



Have fun,

-m
Sally
2016-03-01 07:22:08 UTC
26 Casa Vatoni Sacramento CA
anonymous
2016-09-14 23:45:12 UTC
I am not completely convinced about this one


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...