Created
November 1, 2023 08:42
-
-
Save tolgahanakgun/7f3c5891179f7b4a7c4980a165a5c8eb to your computer and use it in GitHub Desktop.
Send empty reply for web scrapers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# -*- coding: utf-8 -*- | |
# | |
import socket | |
import argparse | |
from ipaddress import ip_address | |
def run_server(ip: ip_address, port: int) -> None: | |
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server.bind((str(ip), port)) | |
server.listen(1) | |
while True: | |
client_sock, _ = server.accept() | |
try: | |
client_sock.close() | |
except: | |
pass | |
def main() -> None: | |
parser = argparse.ArgumentParser(description="Send empty reply for web scrapers") | |
parser.add_argument("-b", "--bind", default="127.0.0.1", type=ip_address, | |
help="Bind server to this IP") | |
parser.add_argument("-p", "--port", default=5666, type=int, | |
help="Bind server to this port") | |
args = parser.parse_args() | |
print(f"Serving on {args.bind} port {args.port} ...") | |
run_server(args.bind, args.port) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment