1. The JSON module provides a very simple way to encode and decode JSON data. Two of the main functions are json.dumps() and json.loads(), which have much fewer interfaces than other serialized libraries such as pickle. The following demonstrates how to convert a Python data structure to JSON:
''' Nobody answered the question? Editor created a Python learning and communication QQ group: 857662006 Look for like-minded friends, help each other, there are good video learning tutorials and PDF e-books in the group! ''' import json data = { 'name' : 'ACME', 'shares' : 100, 'price' : 542.23 } json_str = json.dumps(data)
The following demonstrates how to convert a JSON-encoded string back to a Python data structure:
data = json.loads(json_str)
2. Simple get and post requests, using import requests
import requests response = requests.get('http://httpbin.org/get') print(response.text)
''' Nobody answered the question? Editor created a Python learning and communication QQ group: 857662006 Look for like-minded friends, help each other, there are good video learning tutorials and PDF e-books in the group! ''' # By adding a data parameter to the post request, the data parameter can be constructed from a dictionary. import requests data = { "name":"zhaofan", "age":23 } response = requests.post("http://httpbin.org/post",data=data) print(response.text)
3.GET method and custom header
''' Nobody answered the question? Editor created a Python learning and communication QQ group: 857662006 Look for like-minded friends, help each other, there are good video learning tutorials and PDF e-books in the group! ''' # -* - coding: UTF-8 -* - import urllib2 request = urllib2.Request("http://www.baidu.com/") request.add_header('content-TYPE', 'application/x-www-form-urlencoded') response = urllib2.urlopen(request) print response.getcode() print response.geturl() print response.read()
POST method and custom header
''' //Nobody answered the question? Editor created a Python learning and communication QQ group: 857662006 //Look for like-minded friends, help each other, there are good video learning tutorials and PDF e-books in the group! ''' # -* - coding: UTF-8 -* - import urllib2 import urllib request = urllib2.Request("http://passport.cnblogs.com/login.aspx") request.add_header('content-TYPE', 'application/x-www-form-urlencoded') data={"tbUserName":"test_username", "tbPassword":"test_password"} response = urllib2.urlopen(request, urllib.urlencode(data)) print response.getcode() print response.geturl() print response.read()
4. Writing Practical Test Scripts
''' //Nobody answered the question? Editor created a Python learning and communication QQ group: 857662006 //Look for like-minded friends, help each other, there are good video learning tutorials and PDF e-books in the group! ''' # coding:utf-8 import json import urllib2 import requests class AddScores: def __init__(self): pass def getToken(self): # Get the token value url1 = 'xxxxx'#url r1 = requests.get(url1) self.tokenObj = json.loads(r1.text)#Decoding JSON data if self.tokenObj["result"] == "success": print self.tokenObj["token"] else: print "failed" return self.tokenObj["token"] def personMess(self): # Access to personal information url2 = 'xxx' + self.getToken() r2 = requests.post(url2) print r2.text def addSco(self,resId): # Added Score data = { "memberId": "xxx", "orgCode": "xxx", "resourceId": resId,#Reference, resourceId "configName": "wsp", "resourceType": "wsp" } print "Request parameters for adding scores:" print json.dumps(data)#Coding JSON headers = {'Content-Type': 'application/json'} url3 = 'xxx' + self.getToken() re3 = urllib2.Request(url=url3, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(re3) print response.read()
5. Read and write TXT files
''' //Nobody answered the question? Editor created a Python learning and communication QQ group: 857662006 //Look for like-minded friends, help each other, there are good video learning tutorials and PDF e-books in the group! ''' #coding:utf-8 import time from Demo2.token import AddScores class ResId: def getResId(self): file=open('xxxx') # a=file.read() # print a lId= file.readline() lId=lId.strip(',\n') while lId != '':#Read data line by line print lId addScores = AddScores() addScores.getToken() addScores.personMess() addScores.addSco(lId) time.sleep(68) lId = file.readline() print "=============================" ResId().getResId()