-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocc-backoffer.py
114 lines (78 loc) · 2.99 KB
/
occ-backoffer.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import sys
import time
import json
import requests
import datetime
import argparse
import threading
import traceback
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, quote_plus
from requests.exceptions import ConnectionError
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes("Hello World", "utf8"))
return
def do_POST(self):
try:
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes("Hello World", "utf8"))
return
except Exception as e:
print(e)
traceback.print_exc()
self.send_response(500)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes("Hello World", "utf8"))
return
def run():
print('starting server...')
server_address = ('0.0.0.0', 8080)
httpd = HTTPServer(server_address, RequestHandler)
print('running server...')
httpd.serve_forever()
class OCC:
def __init__(self, url, token, headers, data):
self.url = url
self.token = token
self.headers = headers
self.data = data
self.backoff = 0
self.occ = 0
self.occ_max = 5
self.occ_sleep = 0.5
self.occ_sleep_max = 5
self.occ_sleep_increment = 0.5
self.occ_sleep_increment_max = 5
def post(self):
try:
response = requests.post(self.url, headers=self.headers, data=self.data)
if response.status_code == 200:
self.occ = 0
self.backoff = 0
self.occ_sleep = 0.5
self.occ_sleep_increment = 0.5
return True
elif response.status_code == 429:
self.occ += 1
if self.occ >= self.occ_max:
self.occ = 0
self.backoff += 1
if self.backoff >= 1:
self.backoff = 0
self.occ_sleep += self.occ_sleep_increment
if self.occ_sleep >= self.occ_sleep_max:
self.occ_sleep = 0.5
self.occ_sleep_increment += self.occ_sleep_increment_increment
finally:
time.sleep(self.occ_sleep)
return False