ClientCookie problem - Difference between 'post' on the local network and the internet

Max M maxm at mxm.dk
Fri Oct 22 07:39:05 EDT 2004


I am using ClientCookie for login on to servers and browsing them as 
authenticated users.

I kept getting "HTTP Error 400: Bad Request" errors when submitting my 
forms. So I boiled it down to a simple example.

When I try to use ClientCookie.urlopen() on my private network with ip 
numbers like "http://localhost:8081/test_site/logged_in", it works fine.

If I try to call the same site through the Internet, with a url like: 
http://www.mxm.dk/test_site/logged_in I get the 400 message. But only 
when using "post". 'get' works fine :-s

On the Internet I go out through a gateway, so my external IP is 
different than my internal.

I assume that it causes an url mismatch somewhere. With my local ip 
being set in some header by urlopener, and the server expecting the 
remote ip.

But I have no idea as to how I can avoid it? Any takers?


#########################################

import ClientCookie, urllib

req = ClientCookie.Request
urlopen = ClientCookie.urlopen

def login(came_from, login_form):
     data = {
         'came_from' : came_from,
         '__ac_name' : name,
         '__ac_password' : password,
         '__ac_persistent' : '1',
         'submit' : 'Log ind',
     }
     encoded_data = urllib.urlencode(data)

     request = req(login_form, encoded_data)
     response = urlopen(request)
     content = response.read()
     return content

# This works
came_from = 'http://localhost:8081/test_site/request_tester'
login_form = "http://localhost:8081/test_site/logged_in"
print login(came_from, login_form)

# This results in "HTTP Error 400: Bad Request"
came_from = 'http://www.mxm.dk/test_site/request_tester'
login_form = "http://www.mxm.dk/test_site/logged_in"
print login(came_from, login_form)



############################################
# traceback from the last call to login()

Traceback (most recent call last):
   File "E:\zopes\test_site\PRODUCTS\MXMPRO~1\z.py", line 35, in ?
     print login(came_from, login_form)
   File "E:\zopes\test_site\PRODUCTS\MXMPRO~1\z.py", line 22, in login
     response = urlopen(request)
   File 
"G:\uninstalled\Python\ClientCookie-1.0.0a\ClientCookie\_urllib2_support.py", 
line 715, in urlopen
     return _opener.open(url, data)
   File 
"G:\uninstalled\Python\ClientCookie-1.0.0a\ClientCookie\_Opener.py", 
line 125, in open
     response = meth(req, response)
   File 
"G:\uninstalled\Python\ClientCookie-1.0.0a\ClientCookie\_urllib2_support.py", 
line 387, in http_response
     response = self.parent.error(
   File 
"G:\uninstalled\Python\ClientCookie-1.0.0a\ClientCookie\_Opener.py", 
line 189, in error
     return apply(self._call_chain, args)
   File "C:\pytyons\Python23\lib\urllib2.py", line 306, in _call_chain
     result = func(*args)
   File "C:\pytyons\Python23\lib\urllib2.py", line 412, in 
http_error_default
     raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Python-list mailing list