Abhaykoul commited on
Commit
fcbfedd
·
verified ·
1 Parent(s): 5fd15ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -62
app.py CHANGED
@@ -1,64 +1,49 @@
1
- from flask import Flask, request, jsonify
2
- import requests
3
- import threading
4
- import time
5
- import logging
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- app = Flask(__name__)
8
-
9
- # Set up logging
10
- logging.basicConfig(level=logging.DEBUG)
11
-
12
- def test_proxy(proxy):
13
  try:
14
- start_time = time.time()
15
- response = requests.get('http://httpbin.org/ip', proxies={'http': proxy, 'https': proxy}, timeout=5)
16
- end_time = time.time()
17
- return {
18
- 'proxy': proxy,
19
- 'working': response.status_code == 200,
20
- 'time': round((end_time - start_time) * 1000, 2)
21
- }
22
- except:
23
- return {
24
- 'proxy': proxy,
25
- 'working': False,
26
- 'time': None
27
- }
28
-
29
- def test_proxies_threaded(proxies):
30
- results = []
31
- threads = []
32
-
33
- def worker(proxy):
34
- result = test_proxy(proxy)
35
- results.append(result)
36
-
37
- for proxy in proxies:
38
- thread = threading.Thread(target=worker, args=(proxy,))
39
- threads.append(thread)
40
- thread.start()
41
-
42
- for thread in threads:
43
- thread.join()
44
-
45
- return results
46
-
47
- @app.route('/test-proxies', methods=['POST'])
48
- def test_proxies():
49
- data = request.json
50
- logging.debug(f"Received data: {data}")
51
-
52
- proxies = data.get('proxies', [])
53
-
54
- if not isinstance(proxies, list):
55
- return jsonify({'error': 'Proxies must be a list'}), 400
56
-
57
- if not proxies:
58
- return jsonify({'error': 'No proxies provided'}), 400
59
-
60
- results = test_proxies_threaded(proxies)
61
- return jsonify(results)
62
-
63
- if __name__ == '__main__':
64
- app.run(host='0.0.0.0', port=7860, debug=True)
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from pydantic import BaseModel
4
+ import httpx
5
+ import asyncio
6
+
7
+ app = FastAPI()app = Flask(__name__)
8
+
9
+ # Enable CORS
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["*"], # Allows all origins
13
+ allow_credentials=True,
14
+ allow_methods=["*"], # Allows all methods
15
+ allow_headers=["*"], # Allows all headers
16
+ )
17
+
18
+ class ProxyTest(BaseModel):
19
+ proxy: str
20
+
21
+ @app.post("/test-proxy")
22
+ async def test_proxy(proxy_test: ProxyTest):
23
+ proxy = proxy_test.proxy
24
+ test_url = "http://httpbin.org/ip"
25
+ timeout = 10
26
 
 
 
 
 
 
 
27
  try:
28
+ async with httpx.AsyncClient(proxies={"http://": f"http://{proxy}", "https://": f"http://{proxy}"}) as client:
29
+ start_time = asyncio.get_event_loop().time()
30
+ response = await client.get(test_url, timeout=timeout)
31
+ end_time = asyncio.get_event_loop().time()
32
+
33
+ if response.status_code == 200:
34
+ response_time = round((end_time - start_time) * 1000, 2) # Convert to milliseconds
35
+ return {
36
+ "status": "success",
37
+ "message": f"Proxy {proxy} is working",
38
+ "response_time": f"{response_time} ms"
39
+ }
40
+ else:
41
+ raise HTTPException(status_code=400, detail=f"Proxy test failed with status code: {response.status_code}")
42
+ except httpx.TimeoutException:
43
+ raise HTTPException(status_code=408, detail=f"Proxy {proxy} timed out after {timeout} seconds")
44
+ except Exception as e:
45
+ raise HTTPException(status_code=400, detail=f"Proxy test failed: {str(e)}")
46
+
47
+ if __name__ == "__main__":
48
+ import uvicorn
49
+ uvicorn.run(app, host="0.0.0.0", port=8000) uvicorn.run(app, host="0.0.0.0", port=8000)