1

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?

Chris
  • 44,602
  • 16
  • 137
  • 156
w00
  • 26,172
  • 30
  • 101
  • 147
  • 1
    What library is generating this particular request object? – aychedee Jul 06 '12 at 07:02
  • What code does `self.request.url` come from? Python's urllib2? Or a Python web framework? – Simeon Visser Jul 06 '12 at 07:02
  • @H.Dunlop I have this at the top: **from urlparse import urlparse**. http://docs.python.org/library/urlparse.html --- Really no idea if its standard stuff. Never really touched python before... – w00 Jul 06 '12 at 07:04
  • @w00: please include more code. – Joel Cornett Jul 06 '12 at 07:11
  • You might have to give still more context. urlparse doesn't have anything to do with request objects. Where is this code from? – aychedee Jul 06 '12 at 07:11
  • @H.Dunlop You're right, i removed the import and it still works. I got it from this post: http://stackoverflow.com/questions/2764586/get-current-url-in-python --- I guess it belongs in the Goole App Engine API (i'm using that). But really, any code that gives me the entire URL including all subdomains will do. I'd like to show more code, but i really have no idea what else to show. – w00 Jul 06 '12 at 07:18
  • Ah okay, so it's the GAE request object which inherits from WebOb - http://docs.webob.org/en/latest/reference.html – aychedee Jul 06 '12 at 07:24

3 Answers3

1

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
Vinayak Kolagi
  • 1,831
  • 1
  • 13
  • 26
  • Plain python is fine. But how do i get the URL? It can't be hardcoded. And that's mainly the problem i'm facing right now :) – w00 Jul 06 '12 at 07:39
0

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.

aychedee
  • 24,871
  • 8
  • 79
  • 83
  • Thanks, i tried them all. And they work fine untill i use an URL which has 2 subdomains. Then it's all empty. I'll try the App Engine discussion groups too. Perhaps it's just a bug. I'll also leave this question open for a few hours. You never know what other answer i might get. Otherwise i'll mark your answer as accepted. Thanks anyway :) – w00 Jul 06 '12 at 07:36
  • No worries, that is a bit weird. So what does self.request.host assigned to it? Just the base domain name? – aychedee Jul 06 '12 at 07:47
  • self.request.host gives me the entire URL without http(s):// infront of it. It's just empty when my url contains 2 subdomains. – w00 Jul 06 '12 at 07:57
  • So self.request.host joined with the self.request.scheme would basically give you what you want then? – aychedee Jul 06 '12 at 09:09
  • Yes, if they worked. But they don't when my url consists out of 2 subdomains. – w00 Jul 06 '12 at 09:28
  • Okay, so the self.request object is not getting created properly. It sounds like a bug in whatever code you are using to create the request object. If you show us that code then we might be able to see the problem. There is no inherent bug in WebOb that prevents you using sub, subdomains. – aychedee Jul 06 '12 at 12:19
0

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.

Edgar Allan Pwn
  • 311
  • 1
  • 8