Skip to content

Instantly share code, notes, and snippets.

@newbiethetest
Last active December 31, 2015 07:49
Show Gist options
  • Save newbiethetest/7956276 to your computer and use it in GitHub Desktop.
Save newbiethetest/7956276 to your computer and use it in GitHub Desktop.
# -*- coding:utf8 -*-
import urllib
import curl
import pycurl
import StringIO
def initCurl():
c=pycurl.Curl()
c.setopt(pycurl.FOLLOWLOCATION,0)
c.setopt(pycurl.CONNECTTIMEOUT, 10)
c.setopt(pycurl.MAXREDIRS,1)
c.setopt(pycurl.NOSIGNAL, 1)
return c
def PostData(curl,url,data):
try:
head = ['Accept:*/*',
'Accept-Charset: utf-8;q=0.7,*;q=0.3',
'Accept-Encoding: gzip,deflate',
'Accept-Language:zh-CN,zh;q=0.8',
'Content-Type: application/x-www-form-urlencoded',
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0']
buf = StringIO.StringIO()
curl.setopt(pycurl.WRITEFUNCTION, buf.write)
curl.setopt(pycurl.POSTFIELDS,data)
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.HTTPHEADER,head)
curl.perform()
the_page = buf.getvalue()
#print the_page
buf.close()
return the_page
except pycurl.error,e:
return 'wrong %s'%e
data="php"
host="http://localhost/post.php"
c = initCurl()
data=urllib.urlencode({'1':PostData})
html=PostData(c,host,data)
print html
# my post.php
<?php
if ($_POST["1"]){echo "ok";}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment