Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:55:28+00:00 2026-05-28T05:55:28+00:00

I am downloading data from a server using urllib2. But I need to determine

  • 0

I am downloading data from a server using urllib2. But I need to determine the IP address of the server to which I am connected.

import urllib2
STD_HEADERS = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,
                    */*;q=0.8',
                'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
                'Accept-Language': 'en-us,en;q=0.5',
                'User-Agent': 'Mozilla/5.0 (X11; U; Linux x86_64;en-US;rv:1.9.2.12)     
                           Gecko/20101028 Firefox/3.6.12'}
request = urllib2.Request(url, None, STD_HEADERS)
data =  urllib2.urlopen(request)

Please don’t ask me to find the IP address using the URL as this does not guarantee that the server from which the data is downloaded and the IP address query resolve to the same IP address in case of ‘HTTPRedirects’ or a loadbalancing server

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:55:29+00:00Added an answer on May 28, 2026 at 5:55 am
    import urllib2, socket, urlparse
    
    # set up your request as before, then:
    data = urllib2.urlopen(request)
    addr = socket.gethostbyname(urlparse.urlparse(data.geturl()).hostname)
    

    data.geturl() returns the URL that was used to actually retrieve the resource, after any redirects. The hostname is then fished out with urlparse and handed off to socket.gethostbyname to get the IP address.

    Some hosts may have more than one IP address for a given hostname, so it’s still possible that the request was fulfilled by a different server, but this is as close as you’re gonna get. A gethostbyname right after the URL request is going to use your DNS cache anyway and unless you’re dealing with a time-to-live of, like, 1 second, you’re going to be getting the same server you just used.

    If this is insufficient, you could spin off a thread and do a lsof while still connected to the remote server. I’m sure you could convince urllib2 to leave the connection open for a while so this would succeed. This seems like rather more work than it’s worth, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program is downloading data from a server, once he received all the data,
I am downloading some very large data from a server with the NSURLConnection class.
I am downloading a CSV file from another server as a data feed from
I'm downloading an entire directory from a web server. It works OK, but I
i am downloading tiff images from WAMP server using our Flex AIR client. Using
i need to get data along with images from .net web server. i have
My Javascript application is downloading quite a bit of data from the server and
The Situation: Somewhere in my app I start downloading data from my server. Before
I am developing c# application, in which i am downloading package(zip file) from server
After downloading files from a remote UNIX FTP server, you want to verify that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.