This is my PHP Code with Curl . I need to do the same function using urlfetch in GAE Python. Ho开发者_C百科w can pass all these parameter to urlfetch. Please help me.
$curl = curl_init();
$timeout = 30;
// Logining to my TNT
curl_setopt ($curl, CURLOPT_URL, "https://my.tnt.com/myTNT/login/LoginInitial.do?cmd=1&navigation=1");
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, "userid=aaaa@bb.com&password=1234qwe");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($curl, CURLOPT_COOKIESESSION, 1);
curl_setopt ($curl, CURLOPT_COOKIEFILE, "userid=; password=; JSESSIONID=E1FC9A6D18002370BD4AF7DDBBA617A0; BIGipServermy_tnt_com_pool=2636720036.20480.0000");
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl, CURLOPT_MAXREDIRS, 20);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1");
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($curl, CURLOPT_REFERER, "https://my.tnt.com/myTNT/login/LoginInitial.do");
$text = curl_exec($curl);
$pos = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
This is my python Code.
from google.appengine.api import urlfetch
import urllib
class MainHandler(webapp.RequestHandler):
def get(self):
url = "https://my.tnt.com/myTNT/login/LoginInitial.do?cmd=1&navigation=1"
form_fields = {
"userid": "aaaa@bb.com",
"password": "1234qwe",
}
form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(url=url,
payload=form_data,
method=urlfetch.POST,
validate_certificate='TRUE',
headers={'Host': 'my.tnt.com',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-us,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Keep-Alive': '115',
'Connection': 'keep-alive',
'Referer': 'https://my.tnt.com/myTNT/login/LoginInitial.do',
'Cookie': 'userid=; password=; JSESSIONID=E1FC9A6D18002370BD4AF7DDBBA617A0; BIGipServermy_tnt_com_pool=2636720036.20480.0000',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': '45',
}
)
self.response.out.write(result.final_url)
I'm trying to access the MyTNT webpage. So first i need to login to that page. Above code is for logging into the mytnt website. When i run the PHP Code, it will redirecting to the mytnt home page (https://my.tnt.com/myTNT/landing/landingPage.do). But When i run the python file it was redirecting to the same login page. The Login was unsuccessful when i execute the python file using urlfetch.
I suspect that your php code is using a "cookie jar" like what is available in the mechanize library.
I've used mechanize before for basic scraping of websites, but not actually to log in, so I can't say 100% that it will work for you but I think it's your best shot.
精彩评论