72 lines
1.8 KiB
Python
Executable File
72 lines
1.8 KiB
Python
Executable File
import textwrap
|
|
import random
|
|
import string
|
|
import time
|
|
|
|
|
|
def print_roundtrip(response, *args, **kwargs):
|
|
def format_headers(d): return '\n'.join(f'{k}: {v}' for k, v in d.items())
|
|
print(textwrap.dedent('''
|
|
---------------- request ----------------
|
|
{req.method} {req.url}
|
|
{reqhdrs}
|
|
|
|
{req.body}
|
|
---------------- response ----------------
|
|
{res.status_code} {res.reason} {res.url}
|
|
{reshdrs}
|
|
|
|
{res.text}
|
|
''').format(
|
|
req=response.request,
|
|
res=response,
|
|
reqhdrs=format_headers(response.request.headers),
|
|
reshdrs=format_headers(response.headers),
|
|
))
|
|
|
|
|
|
def ranstr(num):
|
|
salt = ''.join(random.sample(string.ascii_lowercase + string.digits, num))
|
|
return salt
|
|
|
|
|
|
def rannum(num):
|
|
salt = ''.join(random.sample(string.digits, num))
|
|
return salt
|
|
|
|
|
|
def p_in_poly(p, poly):
|
|
px = p['x']
|
|
py = p['y']
|
|
flag = False
|
|
|
|
i = 0
|
|
l = len(poly)
|
|
j = l - 1
|
|
# for(i = 0, l = poly.length, j = l - 1; i < l; j = i, i++):
|
|
while i < l:
|
|
sx = poly[i]['x']
|
|
sy = poly[i]['y']
|
|
tx = poly[j]['x']
|
|
ty = poly[j]['y']
|
|
|
|
# 点与多边形顶点重合
|
|
if (sx == px and sy == py) or (tx == px and ty == py):
|
|
return (px, py)
|
|
|
|
# 判断线段两端点是否在射线两侧
|
|
if (sy < py and ty >= py) or (sy >= py and ty < py):
|
|
# 线段上与射线 Y 坐标相同的点的 X 坐标
|
|
x = sx + (py - sy) * (tx - sx) / (ty - sy)
|
|
# 点在多边形的边上
|
|
if x == px:
|
|
return (px, py)
|
|
# 射线穿过多边形的边界
|
|
if x > px:
|
|
flag = not flag
|
|
j = i
|
|
i += 1
|
|
|
|
# 射线穿过多边形边界的次数为奇数时点在多边形内
|
|
return (px, py) if flag else 'out'
|