0

I have two types of addresses in DataBase:

url_1  = "http://country.city.street/"
url_2  = "http://country.city.street:8180/"

I need to get a uniform address format (url_pattern = "country.city.street") to use in a DNS server. I removed the http:// part from the beginning but can't get a good result with the end of the address. Does anyone have an idea what I could use to get a url_pattern standard?

url_1  = "http://country.city.street/"
url_2  = "http://country.city.street:8180/"

url_1 = url_1[7:]
url_2 = url_2[7:]
4
  • Your current solution is problematic if you have an https://... address. Commented Nov 9, 2021 at 7:19
  • You could use split() a few times: url = address.split('/')[2].split(':')[0]. Commented Nov 9, 2021 at 7:20
  • I know, but this infrastructure use ony http://, https:// will never be used - strange for me ;) Commented Nov 9, 2021 at 7:21
  • Or you could go full-on regular expression: match = re.search(r'//(?P<base>.+?[:/])', address); url = match.group(1). Commented Nov 9, 2021 at 7:23

2 Answers 2

2

There is standard module for URL parsing

from urllib.parse import urlparse
print(urlparse("http://country.city.street:8180/").hostname)
Sign up to request clarification or add additional context in comments.

Comments

2

You can use urllib.parse module. It has a urlparse function that you could use to parse a URL into components.

>>> import urllib.parse
>>> urllib.parse.urlparse("http://country.city.street/")
ParseResult(scheme='http', netloc='country.city.street', path='/', params='', query='', fragment='')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.