Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Web connection delay
#1
Hi,

This link displays the local country data and the server response is immediate:

However, getting the same data from using the below code, takes some time. Why the delay?
TIA

#!/usr/bin/python
import requests as req
from xml.etree import ElementTree as ET
import os, time

def get_tz():
    url = 'http://geoip.ubuntu.com/lookup'
    #read file
    r = req.get(url)
    if(r.status_code == req.codes.ok):
        root = ET.fromstring(r.content)
        # parse an xml file by name
        TZ = "'" + root[12].text + "'"
    t = r.elapsed # returns datetime.timedelta(0, 1, 666890)
    #print('elapsed time: {}'.format(t))
    print(TZ)
    #update time 
    os.environ['TZ'] = TZ
    time.tzset()

get_tz()
Reply
#2
How much time? It's virtually instantaneous for me.
Reply
#3
whaats your topic

User has been warned for this post. Reason: Post garbage "whaats your topic" post to a three-week old thread.
Reply
#4
Quote:micseydel writes: How much time? It's virtually instantaneous for me.
I also find it nearly instantaneous.

Are you going through a firewall?
Reply
#5
The other thing that might be happening is they detected too many requests from your IP + user agent combo and they're rate-limiting you. The solution to that is either wait (it wouldn't surprise me if it works for you now), change your user agent (kinda evil) or figure out the right policy (do they have an API?) and work from there.
Reply
#6
By the way, you can use the find method on an Element.

root = ET.fromstring(r.content)
TZ = resp.find('TimeZone').text
Another trick to make from a flat xml structure a dict:
result = {e.tag: e.text for e in resp}
Output:
{'Ip': 'xxx.xxx.xxx.xxx', 'Status': 'OK', 'CountryCode': 'DE', 'CountryCode3': 'DEU', 'CountryName': 'Germany', 'RegionCode': '07', 'RegionName': 'Nordrhein-Westfalen', 'City': 'Iserlohn', 'ZipPostalCode': '58636', 'Latitude': '51.3800', 'Longitude': '7.7032', 'AreaCode': '0', 'TimeZone': 'Europe/Berlin'}
You should not run your script to often. Otherwise -> RateLimit
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Scraping with some delay Truman 3 3,029 Jun-10-2019, 12:00 AM
Last Post: metulburr
  http.server start delay pastacolsugo 1 2,973 Apr-18-2018, 08:58 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020