-1

enter image description here

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

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • FWIW it'd be a lot easier to use [`requests`](http://python-requests.org) instead of pycurl – Anentropic Dec 19 '14 at 21:31
  • which site did you upgrade to Django 1.7 - mysite.com or the one making the pycurl request? there's no reason why upgrading Django on the requesting site would break the pycurl request out to a third party site – Anentropic Dec 19 '14 at 21:35
  • The one making the pycurl request. Maybe I should have used requests, but I was trying to tranfer code over from php where I was using Curl – user1592380 Dec 19 '14 at 21:49
  • can you curl to mysite.com from the command line? what else have you tried? I don't see any reason to suspect Django – Anentropic Dec 19 '14 at 21:54
  • I'm not saying it is django. – user1592380 Dec 19 '14 at 22:00
  • When you upgraded to Django 1.7, did you upgrade anything else? Like maybe Python, or Curl, or your OS kernel. – macguru2000 Dec 20 '14 at 02:17

1 Answers1

1

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.

macguru2000
  • 2,042
  • 2
  • 18
  • 27