python-Network Programming

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.