I am trying to do a very simple thing, build an URL for a get request that contains a port number and some parameters, as it follows http://localhost:8080/read?date=whatever
I have tried several ways without success, it shouldn't be too difficult but i cannot come up with a solution.
I hope someone helps me, it would be greatly appreciated
Thanks in advance
The previous answer was not to the question you actually asked. Try this:
import urllib
myPort = "8080"
myParameters = { "date" : "whatever", "another_parameters" : "more_whatever" }
myURL = "http://localhost:%s/read?%s" % (myPort, urllib.urlencode(myParameters)) 
Basically, urllib has a function to do what you want, called urlencode. Pass it a dictionary containing the parameter/parameter_value pairs you want, and it will make the proper parameters string you need after the '?' in your url.
Here's a simple generic class that you can (re)use:
import urllib
class URL:
    def __init__(self, host, port=None, path=None, params=None):
        self.host = host
        self.port = port
        self.path = path
        self.params = params
    def __str__(self):
        url = "http://" + self.host
        if self.port is not None:
            url += ":" + self.port
        url += "/"
        if self.path is not None:
            url += self.path
        if self.params is not None:
            url += "?"
            url += urllib.urlencode(self.params)
        return url
So you could do:
url = URL("localhost", "8080", "read", {"date" : "whatever"})
print url
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With