Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. import socket
  2.  
  3.  # create our udp socket
  4.  try:
  5.      socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  6.  except socket.error:
  7.      print("Oops, something went wrong connecting the socket")
  8.      exit()
  9.  
  10.  while 1:
  11.      message = input("> ")
  12.  
  13.      # encode the message
  14.      message = message.encode()
  15.  
  16.      try:
  17.          # send the message
  18.          socket.sendto(message, ("serverip", 9999))
  19.  
  20.          # output the response (if any)
  21.          data, ip = socket.recvfrom(1024)
  22.  
  23.          print("{}: {}".format(ip, data.decode()))
  24.  
  25.      except socket.error:
  26.          print("Error! {}".format(socket.error))
  27.          exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement