urllib2

32 views
Skip to first unread message

hack

unread,
Mar 13, 2012, 9:54:30 PM3/13/12
to Django users
This stuff is killing me. LOL I think I just don't understand the
urllib2 yet.

I'm trying something very simple, but am having a terrible time
figuring out how to get it to work in python and django. All I want
to do is post to my site with params. Here is what I have:

def test(request):
test = " NOTHING "
if request.method=='POST':
test = request.POST.get('test',None)
print 'data posted'
print 'test param: ',test

return HttpResponse("Text only, please."+str(test),
content_type="text/plain")

Here is how I'm trying to call it via IDLE:

>>> parms = urllib.urlencode([('test','testing'),('test2','testing2')])
>>> print parms
test=testing&test2=testing2

>>> response = urllib2.urlopen('http://my.ip.address:8080/myapp/test/',parms)

Heres the error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib2.py", line 124, in urlopen
return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib2.py", line 389, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib2.py", line 502, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib2.py", line 427, in error
return self._call_chain(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib2.py", line 361, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib2.py", line 510, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: FORBIDDEN

Any advice is greatly appreciated. Thanks.

Matt Schinckel

unread,
Mar 13, 2012, 10:09:37 PM3/13/12
to django...@googlegroups.com
That response indicates you do not have permission to access that resource on that server. Are you sure you are hitting the correct URL?

You may want to look into requests, it is a nicer interface for interacting with http servers.

Matt.

hack

unread,
Mar 13, 2012, 10:19:02 PM3/13/12
to django...@googlegroups.com
Hmmm, perhaps this might help: @csrf_exempt

hack

unread,
Mar 13, 2012, 10:24:22 PM3/13/12
to django...@googlegroups.com
Yes, I added @csrf_exempt and not I get a 500 error instead.  I'm not sure what the deal is.

Bill Freeman

unread,
Mar 15, 2012, 11:25:37 AM3/15/12
to django...@googlegroups.com
Are you using the development server? If so, how about sticking a
pdb.set_trace() at line 510 of
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py
to see what you're really getting?

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/qyd4mTfFDZgJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

Alec Taylor

unread,
Mar 15, 2012, 12:10:02 PM3/15/12
to django...@googlegroups.com
Also, quick sort-of side-note:

I recommend checking out the python-requests library as an alternative
to urllib2

http://docs.python-requests.org

Ovnicraft

unread,
Mar 15, 2012, 2:14:24 PM3/15/12
to django...@googlegroups.com
Can you check if development mode, check csrf ?

Regards, 

Any advice is greatly appreciated.  Thanks.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Cristian Salamea
@ovnicraft
Reply all
Reply to author
Forward
0 new messages