w00
w00

Reputation: 26772

Get URL sub subdomain

I'm using the following Python snippet to get a URL:

address = self.request.url

This gives me the URL when it looks like:

http://domain.tld

or

http://sub.domain.tld

But my URL looks like this:

http://sub.sub.domain.tld

In this case the code doesn't return a URL at all. In the end I have to get the first 'sub' domain from the URL. But I can't continue if the code doesn't work when a URL has two subdomains.

Does anyone have any idea how to solve this issue?

Upvotes: 1

Views: 2496

Answers (3)

Edgar Allan Pwn
Edgar Allan Pwn

Reputation: 311

What self.request.url is from seems unanswered. It reminds me of google app engine though, so in the off-chance that you are using GAE, you can use

self.request.host_url 

to get the host url or

self.request.path_url

to get the url without the query parameters.

Source: http://docs.webob.org/en/latest/reference.html

My apologies if this has nothing to do with what you're asking.

Upvotes: 0

Vinayak Kolagi
Vinayak Kolagi

Reputation: 1881

If you are OK with plain python script,

import urlparse
address = urlparse("http://sub.sub.domain.tld")

print address.netloc
print address.path
print address.scheme

Upvotes: 1

aychedee
aychedee

Reputation: 25569

From the information provided I can't give you an answer per se. But I can give some troubleshooting information. If the request.url is empty then there is probably another reason that it is not being populated.

I understand that this is a GAE request object. Which inherits from WebOb.

The request object has a number of other attributes. Try printing them all and seeing what they contain.

print self.request.host
print self.request.host_url
print self.request.scheme   # should be 'http'

If these are all empty then it sounds like the request object is not being created properly. That could be your underlying problem.

Upvotes: 0

Related Questions