Skip to content

Instantly share code, notes, and snippets.

@yashshah1
Created June 8, 2020 08:55
Show Gist options
  • Save yashshah1/a79b520a10c837397106de37a82dbc40 to your computer and use it in GitHub Desktop.
Save yashshah1/a79b520a10c837397106de37a82dbc40 to your computer and use it in GitHub Desktop.
Basic socket
import socket
# DNS runs on port 53
port = 53
# The host for our DNS server is our local host
host = '127.0.0.1'
# SOCK.AF_INET tells socket to use IPv4 addressing
# SOCK.SOCK_DGRAM tells socket to use UDP
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# This will make our program listen on this host and port.
sock.bind((host, port))
# Infinite loop to keep listening on this port.
# We reed 512 bytes because that is the maximum
# For now we are printing whatever request we get.
while True:
data, addr = sock.recvfrom(512)
print(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment