Wednesday, May 23, 2012

Python script - How to set the right server, when I perform request/open of application

Python 3.2.2, I want to check HTTP status code (eg. 200 or 404) on some www application.



There are five servers and each server have a copy of this www application



(The user is automatically redirected to the application on the server with low traffic/sometimes on random server).



Suppose that the name of servers is: 0,1,2,3,4



I tried various options i have tried, three of them I pasted below (they works -always return 200, but choose not this server what I want):



1



import urllib.request,urllib.parse, http.cookiejar
values2= {"server":"2"}
data2 = urllib.parse.urlencode(values2)
data2 = data2.encode("utf-8")
request2 = urllib.request.Request(url,data2)
cj2 = http.cookiejar.CookieJar()
cj2.add_cookie_header(request2)
urlOpener2 = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj2))
url2 = urlOpener2.open(request2)
x2 = url2.getcode()


2



import urllib.request
headers1 = {"Set-Cookie":"server=2"}
req1 = urllib.request.Request(url, None, headers1)
t1 = urllib.request.urlopen(req1)
coderesp1 = t1.getcode()


3



import urllib.request
urlOpener3 = urllib.request.build_opener()
urlOpener3.addheaders.append(("Set-Cookie", "server=2"))
url3 = urlOpener3.open(url)
x3 = url3.getcode()


If you have some questions about my problem, please write, I will immediately reply to what you want to know.





No comments:

Post a Comment