Create an HTTP GET request with port number and parameters

I am trying to do a very simple thing, I create a get request URL that contains a port number and some parameters, as it follows http://localhost:8080/read?date=whatever

I tried several ways without success, it should not be too difficult, but I can not come up with a solution.

I hope someone helps me, it would be very grateful

Thanks in advance

+7
source share
3 answers

The previous answer was not to the question that you really asked. Try the following:

 import urllib myPort = "8080" myParameters = { "date" : "whatever", "another_parameters" : "more_whatever" } myURL = "http://localhost:%s/read?%s" % (myPort, urllib.urlencode(myParameters)) 

Basically, urllib performs the function you need, called urlencode. Pass it a dictionary containing the parameter / value_parameter pairs that you want, and it will make the desired parameter string after "?" in your url.

+12
source

Here is a simple general class that you can use (re):

 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 can do:

 url = URL("localhost", "8080", "read", {"date" : "whatever"}) print url 
+1
source
 data = urllib2.urlopen(url).read() 
-3
source

All Articles