zoukankan      html  css  js  c++  java
  • 【python基础】常见模块:openpyxl & socket & requests

    一、openpyxl,实现随机点名

     1 import openpyxl, random
     2 
     3 def call_somebody():
     4     excel = openpyxl.load_workbook(r"./学员名单.xlsx")
     5     sheet = excel.active
     6     name_list = []  # 装着所有的人名
     7     for name in range(1, 49):
     8         cell = sheet["C" + str(name)]  # "C" + "47"
     9         name_list.append(cell.value)
    10 
    11     return random.choice(name_list)
    12 
    13 print(call_somebody())

    二、socket——send & recieve

     1 from socket import *
     2 
     3 ip = "127.0.0.1"  # 接收方的 ip地址, 本机: 127.0.0.1, localhost
     4 port = 4444  # 端口号, 65535, 1~1023, 1024--> 以后设置
     5 address = (ip, port)  # 接收方的地址
     6 
     7 # 创建一个socket对象
     8 s = socket(AF_INET, SOCK_DGRAM)
     9 
    10 while True:
    11     # 发送数据
    12     # info = "xixi"
    13     info = input("请输入你要发送的内容:")
    14     if info:
    15         s.sendto(info.encode('utf-8'), address)
    16     else:
    17         break
    18 # 发送完毕 --> 关闭
    19 s.close()
     1 from socket import *
     2 
     3 # 初始化操作
     4 ip = "127.0.0.1"  # 接收方的ip
     5 port = 4444  # 接收方端口
     6 address = (ip, port)
     7 
     8 # 创建socket对象
     9 s = socket(AF_INET, SOCK_DGRAM)
    10 
    11 # 绑定接收地址
    12 s.bind(address)
    13 
    14 while True:
    15     # 接收数据
    16     data, adr = s.recvfrom(1024)
    17     print("接收到{}发送的信息, 信息的内容是:{}".format(str(adr), str(data, 'utf-8')))

    三、requests

     1 import requests, base64
     2 
     3 
     4 # 获取access_token
     5 def get_access_token():
     6     url = "https://aip.baidubce.com/oauth/2.0/token?" 
     7           "grant_type=client_credentials&" 
     8           "client_id=SdnZagCfatQDEtjoPIKhf2zR&" 
     9           "client_secret=bL1bA9Gs3HnSoGfiR3Mix88Gg3YjIURI"
    10     r = requests.post(url)  # 发起请求, 获取响应
    11     res = r.json()  # r.text, r.content.decode('utf-8'), r.json() 获取响应的json内容
    12     # print(type(res))  # <class 'dict'>
    13     # print(res)
    14     return res.get('access_token')  # 返回值为access_token的值
    15 
    16 
    17 def detect_face():
    18     # 请求的地址
    19     url = "https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=%s" % get_access_token()
    20 
    21     header = {"Content-Type": "application/json"}
    22 
    23     with open(r"./1.jpg", "rb") as f:
    24         b64_img = base64.b64encode(f.read())
    25 
    26     data = {
    27         "image": b64_img,
    28         "image_type": "BASE64",
    29         "face_field": "age,beauty,expression,face_shape,gender,glasses"
    30     }
    31 
    32     # 发起请求 --> 获取响应对象r
    33     r = requests.post(url, headers=header, data=data)
    34     print(r.json())  # 获取响应的json格式数据
    35 
    36 
    37 detect_face()
  • 相关阅读:
    自定义的类型放入STL的set中,需要重载自定义类中的“<”符号(转)
    C++中的explicit关键字(转)
    【小米3使用经验】小米3关闭系统自动更新(升级)
    享元模式FlyweightPattern(转)
    程序员的工作环境与效率
    Windows7远程登陆访问2003很卡的解决办法
    windows7上可以正常安装的VS2010版本
    使用Visual Studio指定命令行参数
    winform配置文件的简单使用
    Java nio epoll mina
  • 原文地址:https://www.cnblogs.com/Tree0108/p/12116917.html
Copyright © 2011-2022 走看看