Network Programming
from urllib import request,parse
url = ' http://www.nnzhp.cn '
req = Request.urlopen (URL) # open a URL, send a get request
Content = Req.read (). Decode () # get return results
FW = open (' baidu.html ',' W ', encoding=' Utf-8 ')
Fw.write (content)
ImportJson
Url=' Http://api.nnzhp.cn/api/user/stu_info?stu_name=xiaohei '
req = Request.urlopen (URL)#Open aURL,HairGetRequest
Content = Req.read (). Decode ()#Get return results
Res_dic = json.loads (content)#the returned result is converted into a dictionary
ifRes_dic.get (' Error_code ') = = 0:
Print'Test Pass')
Else:
Print'Test Failed', Res_dic)
URL =' Http://api.nnzhp.cn/api/user/login '
data = {
' username ':' admin ',
' passwd ':' aA123456 '
}#Request Data
data = Parse.urlencode (data)#urlencode,automatically spell the parameters for you.
# xx=xx&xx=11
req = Request.urlopen (Url,data.encode ())#HairPostRequest, The data type must first be converted to byte type
Print (Req.read (). Decode ()) #return results converted to common types
python-Network Programming