hyb
2025-05-20 e8003b5c66494c398fa8b716e0872771e2ea4af8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# !/usr/bin/python3
# -*- coding: utf-8 -*-
import datetime
import os
import random
import string
import time
 
from requests_toolbelt import MultipartEncoder
 
from httprunner.compat import builtin_str, integer_types
from httprunner.exceptions import ParamsError
 
 
def gen_random_string(str_len):
    """generate random string with specified length"""
    return "".join(
        random.choice(string.ascii_letters + string.digits) for _ in range(str_len)
    )
 
 
def get_timestamp(str_len=13):
    """get timestamp string, length can only between 0 and 16"""
    if isinstance(str_len, integer_types) and 0 < str_len < 17:
        return builtin_str(time.time()).replace(".", "")[:str_len]
 
    raise ParamsError("timestamp length can only between 0 and 16.")
 
 
def get_current_date(fmt="%Y-%m-%d"):
    """get current date, default format is %Y-%m-%d"""
    return datetime.datetime.now().strftime(fmt)
 
 
def multipart_encoder(field_name, file_path, file_type=None, file_headers=None):
    if not os.path.isabs(file_path):
        file_path = os.path.join(os.getcwd(), file_path)
 
    filename = os.path.basename(file_path)
    with open(file_path, "rb") as f:
        fields = {field_name: (filename, f.read(), file_type)}
 
    return MultipartEncoder(fields)
 
 
def multipart_content_type(multipart_encoder):
    return multipart_encoder.content_type
 
 
""" built-in hooks
"""
 
 
def setup_hook_prepare_kwargs(request):
    pass