作者在 2016-08-09 16:21:48 发布以下内容
#-*- coding:UTF-8 -*-
import urllib2
import urllib
import cookielib
import re
login_url = 'http://192.168.2.127:8080/shopweb/login'
bind_url = 'http://192.168.2.127:8080/shopweb/bind'; # 登陆用户名和密码
data={
"username":"chengtao",
"password":"000000"
} # urllib进行编码
binddata=[{
"eslId": "54-72-BA-99",
"goodsId":"6/###/1000097"
}]
# bind_data=urllib.quote_plus(str(binddata))
bind_data=urllib.urlencode(str(binddata))
print bind_data
post_data=urllib.urlencode(data)
print post_data
headers = {'Content-Type' : 'text/plain'} # 发送头信息
post_headers=urllib.urlencode(headers)
print type(post_headers)
cookie=cookielib.CookieJar() # 初始化一个CookieJar来处理Cookie
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) # 实例化一个全局opener
req=urllib2.Request(login_url,post_data) # 获取cookie
result = opener.open(req)
print result.read()
for item in cookie:
print 'Name = '+item.name
print 'Value = '+item.value #获取cookie值
ck = item.name +"=" +item.value
print ck
# print type(ck)
# bresult = opener.open(bind_url,bind_data) # 访问主页 自动带着cookie信息
# print bresult.read() # 显示结果
# opener.addheaders.append(('Cookie','cookiename=cookievalue')) #cookie
opener.addheaders.append(('Cookie',ck))
bindrequest = urllib2.Request(
url = "http://192.168.2.127:8080/shopweb/bind",
data = urllib.urlencode(binddata))
ff = opener.open(bindrequest)
print "----return request---- \n"
print ff.read()