Created
November 9, 2014 23:52
-
-
Save pvsousalima/2a0200a3d853a44b4333 to your computer and use it in GitHub Desktop.
Simple server to test pings
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
# UDPPingerServer.py | |
# We will need the following module to generate randomized lost packets import random | |
from socket import * | |
# Create a UDP socket | |
# Notice the use of SOCK_DGRAM for UDP packets | |
import random | |
serverSocket = socket(AF_INET, SOCK_DGRAM) # Assign IP address and port number to socket | |
serverSocket.bind(('', 12000)) | |
while True: | |
# Generate random number in the range of 0 to 10 | |
rand = random.randint(0, 10) | |
# Receive the client packet along with the address it is coming from | |
message, address = serverSocket.recvfrom(1024) | |
# Capitalize the message from the client | |
message = message.upper() | |
# If rand is less is than 4, we consider the packet lost and do not respond | |
if rand < 4: | |
continue | |
# Otherwise, the server responds | |
serverSocket.sendto(message, address) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment