python--Network Programming

Source: Internet
Author: User

# import Urllib.request
# import JSON
Import requests

# url = ' http://api.nnzhp.cn/api/user/stu_info?stu_name=ybq123 '
# res = urllib.request.urlopen (URL) # Send request
# res_new = Res.read (). Decode () # Get Results
# Print (res_new)

# Send a GET request
# res = requests.get (URL) # send GET request
# print (res.text) # Get Results
# Print (Type (res.text))
# Print (Json.loads (res.text))
# Print (Res.json ()) # Gets the result directly is the dictionary, must return is the JSON string, can use the JSON () method

# Send a POST request
# url = ' Http://api.nnzhp.cn/api/user/login '
# data = {' username ': ' Niuhanyang ', ' passwd ': ' aA123456 '}
# req = requests.post (URL, data) # sends a POST request, the first parameter is the URL, the second parameter is the requested data
# Print (Req.json ())
# Print (Req.text)
#
# url1 = ' Http://api.nnzhp.cn/api/user/user_reg '
# data = {
# ' username ': ' Wangsilei ',
# ' pwd ': ' wang123a ',
# ' cpwd ': ' wang123a '
# }
# res = Requests.post (URL1, data)
# Print (Res.text)

# The import parameter is JSON
# url = ' Http://api.nnzhp.cn/api/user/add_stu '
# data = {
# ' name ': ' Wang Silei ',
# ' Grade ': ' Sagittarius ',
# ' phone ': 18033555656
# }
# req = requests.post (URL, json=data)
# Print (Req.json ())

# Add a cookie
# 8b7d1b49fc8be17afe68bc67c82ea98b
# url = ' Http://api.nnzhp.cn/api/user/gold_add '
# data = {
# ' stu_id ': 231,
# ' Gold ': 10000
# }
# cookie = {' Niuhanyang ': ' 8b7d1b49fc8be17afe68bc67c82ea98b '}
# req = requests.post (URL, data, Cookies=cookie)
# Print (Req.json ())

# Add Header's
url = ' Http://api.nnzhp.cn/api/user/all_stu '
Header = {' Referer ': ' http://api.nnzhp.cn/'}
req = Requests.get (URL, headers=header)
Print (Req.json ())

# Upload Files
# url = ' Http://api.nnzhp.cn/api/file/file_upload '
# with Open (R ' C:\Users\wangsilei\Music\ Phoenix legend \ Phoenix Legends-Fury of Love. mp3 ', ' RB ') as F:
# r = requests.post (URL, files={' file ': F})
# Print (R.json ())

# download file
# url = ' http://img.lelezone.com/thumb/p/imgs/20/20390/20390_10.jpg '
# r = Requests.get (URL)
# print (r.status_code) # Get the requested status code
# print (r.content) # Gets the returned result in binary format
# with open (' dog.jpg ', ' WB ') as F:
# F.write (r.content)

# Save Web Page
# url = ' http://www.nnzhp.cn/archives/630 '
# res = requests.get (URL)
# with open (' nnzhp_630.html ', ' WB ') as F:
# F.write (res.content)

python--Network Programming

Related Article

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.