Home  >  Article  >  Backend Development  >  python使用urllib2提交http post请求的方法

python使用urllib2提交http post请求的方法

WBOY
WBOYOriginal
2016-06-06 11:17:461332browse

本文实例讲述了python使用urllib2提交http post请求的方法。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/python 
#coding=utf-8 
import urllib 
import urllib2 
def post(url, data): 
  req = urllib2.Request(url) 
  data = urllib.urlencode(data) 
  #enable cookie 
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) 
  response = opener.open(req, data) 
  return response.read() 
def main(): 
  posturl = "http://yourwebname/member/login" 
  data = {'email':'myemail', 'password':'mypass', 'autologin':'1', 'submit':'登 录', 'type':''} 
  print post(posturl, data) 
if __name__ == '__main__': 
  main() 

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn