I am working on a django website which appeared to be working with django 1.64 (although only tested once). In my app's view I have a function contact which when a form that is submitted is supposed to message a website ("mysite").
def contact(request):
if request.method == 'POST':
import urllib
f = { 'body': str(request.POST) }
post_data = urllib.urlencode(f)
curl_to_site(post_data)
def curl_to_site(post_data):
import pycurl
import cStringIO
buf = cStringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'mysite.com')
c.setopt(pycurl.SSL_VERIFYPEER, False)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.VERBOSE, 1)
c.setopt(pycurl.COOKIEJAR, 'cookie.txt')
c.setopt(pycurl.COOKIEFILE, 'cookie.txt')
c.setopt(pycurl.USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201")
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.TIMEOUT, 5)
c.setopt(pycurl.CONNECTTIMEOUT, 6)
c.setopt(c.WRITEFUNCTION, buf.write)
c.perform()
c.close()
print buf.getvalue()
buf.close()
return
When I submit the form The request hangs forever. please see screenshot. The Curl output is :
* Hostname was NOT found in DNS cache
* Trying xxx.xx.xx.xx...
* Connected to *****.com (xxx.xx.xx.xx) port 80 (#0)
> POST /index/e HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201
Host: to *****.com
Accept: */*
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
< HTTP/1.1 100 Continue
This did work once. I've added a lot more options but I'm not sure if the reason for failure is an upgrade to django 1.7 or if this is unrelated. How can I fix this and get it going again?
EDIT:
I replaced:
curl_to_site(post_data)
with:
import requests
r = requests.post('mysite.com', data=post_data)
and it worked right away , so I never got back to pycurl. I had to move on to the next stage of the progect so I didn't get back to pycurl. However it definitively was not the upgrade to django 1.7
I would first try to open up a python shell, import pycurl and then run your curl command manually. Open your terminal, run python and type this:
import pycurl
import cStringIO
buf = cStringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'mysite.com')
c.setopt(pycurl.SSL_VERIFYPEER, False)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.VERBOSE, 1)
c.setopt(pycurl.COOKIEJAR, 'cookie.txt')
c.setopt(pycurl.COOKIEFILE, 'cookie.txt')
c.setopt(pycurl.USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201")
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.TIMEOUT, 5)
c.setopt(pycurl.CONNECTTIMEOUT, 6)
c.setopt(c.WRITEFUNCTION, buf.write)
c.perform()
c.close()
Does it work? If not, we now know that the Django upgrade is not responsible for your problem.
Next I would check the response content of your failed request (POST /contact/), if Django is in debug mode then you may get a full stack-trace of why the request was aborted. Though, an aborted request may not have any content at all. Still this is something you should check.
So, next I would make sure that you can ping mysite.com, if not, check mysite.com's dns rules and the web hosting config. You should also ssh into the server you make the curl call from and make sure it can ping your destination server (mysite.com).
Let me know if any of that helped, I hope it did.