SHARE
TWEET

Untitled

a guest Oct 18th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import struct
  3. from socket import *
  4. import sys
  5.  
  6. def mainMenu():
  7.     print("1. Nastavit PC ako klienta")
  8.     print("2. Nastavit PC ako server")
  9.     print("3. Vypnut")
  10.     selection = int(input("Vyber:"))
  11.     if selection==1:
  12.         start_client()
  13.     elif selection==2:
  14.         start_server()
  15.     elif selection==3:
  16.         exit
  17.     else:
  18.         print("Vyber cislo z menu!")
  19.         mainMenu()
  20.  
  21. def start_client():
  22.     content = None
  23.     def client_menu1():
  24.         host = input("Zadaj IP adresu kam chces posielat v tvare xxx.xxx.xxx.xxx\n")
  25.         return host
  26.  
  27.     # file_name = print("Zadaj nazov suboru aj s cestou\n")
  28.     # with open('file_name', 'rb') as f:
  29.     #     content = f.read()
  30.     host = client_menu1()
  31.     # host = "127.0.0.1"  # set to IP address of target computer
  32.     print(host)
  33.     port = 10051
  34.     addr = (host, port)
  35.     UDPSock = socket(AF_INET, SOCK_DGRAM)
  36.  
  37.     def header_connection_msg():
  38.         type = 0 #0 connection, 1 - info, 2 - data_head
  39.         size_fragments = 1500#user_edit
  40.         num_fragments = 1 #sizeofMSG/sizeoffragments
  41.         type_of_msg = 0 #0 - text, 1 - file
  42.         #checksum = CRC
  43.         header = struct.pack("IHHI", type, size_fragments, num_fragments, type_of_msg)
  44.         return header
  45.  
  46.     datap = 0
  47.  
  48.     if UDPSock.sendto(header_connection_msg(), addr):
  49.         (datap, addr) = UDPSock.recvfrom(1024)
  50.  
  51.     if datap == "0":
  52.         print("Nic")
  53.     else:
  54.         print("OK")
  55.  
  56.     while True:
  57.          data = input("Zadaj spravu")
  58.          UDPSock.sendto(data.encode(), addr)
  59.          (datap, addr) = UDPSock.recvfrom(1024)
  60.          print(datap.decode())
  61.          if data == "exit":
  62.              break
  63.  
  64.     UDPSock.close()
  65.     os._exit(0)
  66.  
  67.  
  68. def start_server():
  69.     host = "127.0.0.1"
  70.     port = 10051
  71.     buf = 1024
  72.  
  73.     address = (host, port)
  74.     UDPSock = socket(AF_INET, SOCK_DGRAM)
  75.     (header, address) = UDPSock.recvfrom(1024)
  76.     type, size_fragments, num_fragments, type_of_msg = struct.unpack("cHHc", header)
  77.     p = 0
  78.     if type == 0:
  79.         p = "0"
  80.     UDPSock.sendto(p.encode(), address)
  81.     UDPSock.bind(address)
  82.     print('Server caka')
  83.  
  84.     while True:
  85.         (data, address) = UDPSock.recvfrom(5)
  86.         data1 = "Server prijal spravu"
  87.         UDPSock.sendto(data1.encode(), address)
  88.         new_data = data.decode()
  89.         print(f"Received message: {sys.getsizeof(data)}")
  90.         print(f"Received message: {data}")
  91.         if data == b"exit":
  92.             break
  93.  
  94.     UDPSock.close()
  95.     os._exit(0)
  96.  
  97. mainMenu()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top