Skip to content

Instantly share code, notes, and snippets.

@buesing
Last active December 16, 2015 13:48
Show Gist options
  • Save buesing/5444028 to your computer and use it in GitHub Desktop.
Save buesing/5444028 to your computer and use it in GitHub Desktop.
from socket import *
s = raw_input("listen for TCP or UDP?\n")
if s.upper() == "TCP":
tcp = socket(AF_INET, SOCK_STREAM)
tcp.setsockopt(SOCK_STREAM, SO_REUSEADDR, 1)
tcp.bind(('', 12000))
tcp.listen(1)
print("listening for TCP")
while True:
communication, addr = tcp.accept()
data = 1
while data:
data = communication.recv(1024)
print(data)
# return data to client
communication.send(data)
tcp.close()
else:
print("listening for UDP")
udp = socket(AF_INET, SOCK_DGRAM)
udp.bind(('', 12001))
while True:
udpdata, addr = udp.recvfrom(1024)
if udpdata:
udp.sendto(udpdata, addr)
continue
udp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment