-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
331 lines (266 loc) · 12.6 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# from fastapi import FastAPI, File, UploadFile, HTTPException
# import pandas as pd
# from io import StringIO
# import smtplib
# import validators
# import dns.resolver
# from fastapi.middleware.cors import CORSMiddleware
# import asyncio
# from fastapi.responses import JSONResponse
# import os
# import ssl
# app = FastAPI()
# origins = [
# # "https://email-validation-fr.vercel.app"
# # "https://email-val.netlify.app/"
# # "https://email-validation-90.pages.dev"
# "http://localhost:3000"
# ]
# app.add_middleware(
# CORSMiddleware,
# allow_origins=origins, # Allows the frontend origin
# allow_credentials=True, # Allows cookies or credentials
# allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"], # Allowed HTTP methods
# allow_headers=["Content-Type", "Authorization"], # Allowed headers
# )
# ssl_context = ssl.create_default_context()
# mx_cache = {}
# def check_mx_records(domain):
# if domain in mx_cache:
# return mx_cache[domain]
# try:
# records = dns.resolver.resolve(domain, 'MX')
# for record in records:
# mx_cache[domain] = str(record.exchange)
# return mx_cache[domain]
# except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN, dns.resolver.Timeout, dns.resolver.NoNameservers):
# mx_cache[domain] = None
# return None
# def verify_email_sync(email):
# check_validate = validators.email(email)
# if not check_validate:
# return {"email": email, "is_valid": False}
# domain_split = email.split('@')[-1]
# email_not_valid = email.split('@')[0]
# email_not_valid_2=email.split('.')[-1]
# if email_not_valid_2 in ["edu","gov","org",]:
# return {"email": email, "is_valid": False}
# if email_not_valid in ["helpdesk","volunteers","office","customer","customercare","privacy","enquiry","inquiry","order","info","mail","admin","supportteam","notice","partner","partnership","services","service","commercial","webmaster","postmaste","hola","post","welcome","example","invoice","advise","admission","communication","ventas","kontakt","contacto","client","terms","donate","promo","promotion","project","feedback","hr","sample","online","function","member","membership","reception","reservation","support","account","hello","career","resume","recovery","whois","domain","proxy","registration","admin","shop","hi","demo","template","hosting","assistenza","atendimento","commerciale","generalinfo","subscribe","noreply","support","contact","payment","payroll","abuse","billing","submission","spam","write","emails"]:
# return {"email": email, "is_valid": False}
# if domain_split in ["gmail.com","yahoo.com","hotmail.com","outlook.com","aol.com","abc.com","xyz.com","godaddy.com","email.com"]:
# return {"email": email, "is_valid": False}
# mx_host = check_mx_records(domain_split)
# if not mx_host:
# return {"email": email, "is_valid": False}
# try:
# server = smtplib.SMTP(mx_host)
# server.set_debuglevel(0)
# server.helo()
# server.mail("[email protected]")
# code, _ = server.rcpt(email)
# server.quit()
# return {"email": email, "is_valid": code == 250}
# except:
# return {"email": email, "is_valid": False}
# async def verify_email(email):
# loop = asyncio.get_event_loop()
# return await loop.run_in_executor(None, verify_email_sync, email)
# @app.post("/message")
# async def main(file: UploadFile = File(...)):
# try:
# contents = await file.read()
# df = pd.read_csv(StringIO(contents.decode('utf-8')))
# if 'email' not in df.columns:
# raise HTTPException(status_code=400, detail="CSV file must contain 'email' column")
# emails = df['email'].tolist()
# batch_size = 6
# results = []
# for i in range(0, len(emails), batch_size):
# batch = emails[i:i+batch_size]
# tasks = [verify_email(email) for email in batch]
# results = await asyncio.gather(*tasks)
# results.extend(results)
# # df['is_valid'] = [result['is_valid'] for result in results]
# df[ 'is_valid'] = [result['is_valid'] for result in results]
# # tasks = [verify_email(email) for email in emails]
# # results = await asyncio.gather(*tasks)
# # df['is_valid'] = [result['is_valid'] for result in results]
# return df.to_dict(orient="records")
# except Exception as e:
# raise HTTPException(status_code=500, detail=str(e))
# if __name__ == "__main__":
# import uvicorn
# port = int(os.environ.get("PORT", 8000))
# uvicorn.run(app, host="0.0.0.0", port=port)
from fastapi import FastAPI, File, UploadFile, HTTPException
import logging
import pandas as pd
from io import StringIO
import smtplib
import validators
import dns.resolver
from fastapi.middleware.cors import CORSMiddleware
import asyncio
from fastapi.responses import JSONResponse
import os
import redis.asyncio as aioredis
from aiosmtplib import SMTP
import ssl
from concurrent.futures import ThreadPoolExecutor
app = FastAPI()
origins = [
# "https://email-validation-fr.vercel.app"
# "https://email-val.netlify.app/"
# "https://email-validation-90.pages.dev"
"http://52.66.255.242"
]
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
ssl_context = ssl.create_default_context()
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=["Content-Type", "Authorization"],
)
mx_cache = {}
semaphore = asyncio.Semaphore(50)
async def check_mx_records(domain):
if domain in mx_cache:
return mx_cache[domain]
try:
records = dns.resolver.resolve(domain, 'MX')
for record in records:
mx_host = str(record.exchange)
mx_cache[domain] = mx_host
return mx_host
except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN, dns.resolver.Timeout, dns.resolver.NoNameservers):
mx_cache[domain] = None
return None
ssl_create = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
async def verify_email_sync(email):
check_validate = validators.email(email)
if not check_validate:
logger.debug(f"Email validation failed: {email}")
return {"email": email, "is_valid": False}
domain_split = email.split('@')[-1]
email_not_valid = email.split('@')[0]
email_not_valid_2 = email.split('.')[-1]
if email_not_valid_2 in ["edu", "gov", "org"]:
return {"email": email, "is_valid": False}
if email_not_valid in ["helpdesk", "volunteers", "office", "customer", "customercare", "privacy", "enquiry", "inquiry", "order", "info", "mail", "admin", "supportteam", "notice", "partner", "partnership", "services", "service", "commercial", "webmaster", "postmaste", "hola", "post", "welcome", "example", "invoice", "advise", "admission", "communication", "ventas", "kontakt", "contacto", "client", "terms", "donate", "promo", "promotion", "project", "feedback", "hr", "sample", "online", "function", "member", "membership", "reception", "reservation", "support", "account", "hello", "career", "resume", "recovery", "whois", "domain", "proxy", "registration", "admin", "shop", "hi", "demo", "template", "hosting", "assistenza", "atendimento", "commerciale", "generalinfo", "subscribe", "noreply", "support", "contact", "payment", "payroll", "abuse", "billing", "submission", "spam", "write", "emails"]:
return {"email": email, "is_valid": False}
if domain_split in ["gmail.com", "yahoo.com", "hotmail.com", "outlook.com", "aol.com", "abc.com", "xyz.com", "godaddy.com", "email.com"]:
return {"email": email, "is_valid": False}
mx_host = await check_mx_records(domain_split)
if not mx_host:
logger.debug(f"MX host not found for {email}")
return {"email": email, "is_valid": False}
try:
async with SMTP(hostname=mx_host,port=465,tls_context=ssl_context,timeout=40,use_tls=True) as server:
await asyncio.wait_for(server.connect(), timeout=40)
# await server.starttls(ssl_context=ssl_context)
# await server.helo()
# await server.connect()
await server.mail("[email protected]")
code, _ = await asyncio.wait_for(server.rcpt(email), timeout=40)
logger.debug(f"SMTP RCPT code: {code} for {email}")
return {"email": email, "is_valid": code == 250}
except Exception as e:
logger.error(f"SMTP verification failed for {email}, error: {str(e)}")
return {"email": email, "is_valid": False}
executor = ThreadPoolExecutor(max_workers=20)
async def verify_email(email):
return await verify_email_sync(email)
@app.post("/message")
async def main(file: UploadFile = File(...)):
try:
logger.info("received file")
contents = await file.read()
df = pd.read_csv(StringIO(contents.decode('utf-8')))
logger.debug(f"CSV contents: {df.head()}")
if 'email' not in df.columns:
raise HTTPException(status_code=400, detail="CSV file must contain 'email' column")
emails = df['email'].tolist()
logger.info(f"Validating emails: {emails}")
batch_size = 25
results = []
for i in range(0, len(emails), batch_size):
batch = emails[i:i + batch_size]
logger.info(f"Processing batch: {batch}")
tasks = [verify_email(email) for email in batch]
batch_results = await asyncio.gather(*tasks)
results.extend(batch_results)
await asyncio.sleep(1)
logger.info(results)
df['is_valid'] = [result['is_valid'] for result in results]
logger.info("Validation completed successfully")
return df.to_dict(orient="records")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
port = int(os.environ.get("PORT", 8000))
uvicorn.run(app, host="0.0.0.0", port=port)
# import boto3
# from fastapi import FastAPI, File, UploadFile, HTTPException
# import logging
# import pandas as pd
# from io import StringIO
# from fastapi.middleware.cors import CORSMiddleware
# import asyncio
# app = FastAPI()
# origins = [
# "http://52.66.255.242"
# ]
# logging.basicConfig(level=logging.INFO)
# logger = logging.getLogger(__name__)
# app.add_middleware(
# CORSMiddleware,
# allow_origins=origins,
# allow_credentials=True,
# allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
# allow_headers=["Content-Type", "Authorization"],
# )
# # Set up the SES client
# ses_client = boto3.client('ses', region_name='us-east-1') # Replace with your SES region
# async def verify_email_with_ses(email):
# try:
# # Email verification logic
# response = ses_client.verify_email_identity(EmailAddress=email)
# logger.info(f"Verification email sent to {email}")
# return {"email": email, "is_valid": True}
# except Exception as e:
# logger.error(f"SES email verification failed for {email}, error: {str(e)}")
# return {"email": email, "is_valid": False}
# @app.post("/message")
# async def main(file: UploadFile = File(...)):
# try:
# logger.info("received file")
# contents = await file.read()
# df = pd.read_csv(StringIO(contents.decode('utf-8')))
# if 'email' not in df.columns:
# raise HTTPException(status_code=400, detail="CSV file must contain 'email' column")
# emails = df['email'].tolist()
# logger.info(f"Validating emails: {emails}")
# batch_size = 25
# results = []
# for i in range(0, len(emails), batch_size):
# batch = emails[i:i + batch_size]
# logger.info(f"Processing batch: {batch}")
# tasks = [verify_email_with_ses(email) for email in batch]
# batch_results = await asyncio.gather(*tasks)
# results.extend(batch_results)
# df['is_valid'] = [result['is_valid'] for result in results]
# logger.info("Validation completed successfully")
# return df.to_dict(orient="records")
# except Exception as e:
# raise HTTPException(status_code=500, detail=str(e))
# if __name__ == "__main__":
# import uvicorn
# import os
# port = int(os.environ.get("PORT", 8000))
# uvicorn.run(app, host="0.0.0.0", port=port)