Last active
December 16, 2015 13:48
-
-
Save buesing/5444028 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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