Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import socket
  3. import select
  4. import errno
  5. import datetime
  6.  
  7. HEADER_LENGTH = 10
  8.  
  9. IP = "192.168.0.172"
  10. PORT = 6969
  11. startCH = ""
  12. endCH = ""
  13. my_username = startCH + "FIAS" + endCH #input("Username: ")
  14. x = datetime.datetime.now()
  15.  
  16. # Create a socket
  17. # socket.AF_INET - address family, IPv4, some otehr possible are AF_INET6, AF_BLUETOOTH, AF_UNIX
  18. # socket.SOCK_STREAM - TCP, conection-based, socket.SOCK_DGRAM - UDP, connectionless, datagrams, socket.SOCK_RAW - raw IP packets
  19. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  20.  
  21. # Connect to a given ip and port
  22. client_socket.connect((IP, PORT))
  23.  
  24. # Set connection to non-blocking state, so .recv() call won;t block, just return some exception we'll handle
  25. client_socket.setblocking(False)
  26.  
  27. # Prepare username and header and send them
  28. # We need to encode username to bytes, then count number of bytes and prepare header of fixed size, that we encode to bytes as well
  29.  
  30. #username = my_username.encode('utf-8')
  31. #username_header = f"{len(username):<{HEADER_LENGTH}}".encode('utf-8')
  32. #client_socket.send(username_header + username)
  33.  
  34. msg = startCH + "LD|DA" + x.strftime("%y%m%d") + "|TI" + x.strftime("%H%M%S") + "|V#1.99|IFWW|" + endCH
  35. print(msg)
  36. ifcmsg = msg.encode('utf-8')
  37. print(ifcmsg)
  38. msg_header = f"{len(ifcmsg):<{HEADER_LENGTH}}".encode('utf-8')
  39. client_socket.send(msg_header,ifcmsg)
  40.  
  41.  
  42. while True:
  43.  
  44. # Wait for user to input a message
  45. message = input(f'')
  46.  
  47. # If message is not empty - send it
  48. if message:
  49.  
  50. # Encode message to bytes, prepare header and convert to bytes, like for username above, then send
  51. message = message.encode('utf-8')
  52. message_header = f"{len(message):<{HEADER_LENGTH}}".encode('utf-8')
  53. client_socket.send(message_header + message)
  54.  
  55. try:
  56. # Now we want to loop over received messages (there might be more than one) and print them
  57. while True:
  58.  
  59. # Receive our "header" containing username length, it's size is defined and constant
  60. username_header = client_socket.recv(HEADER_LENGTH)
  61.  
  62. # If we received no data, server gracefully closed a connection, for example using socket.close() or socket.shutdown(socket.SHUT_RDWR)
  63. if not len(username_header):
  64. print('Connection closed by the server')
  65. sys.exit()
  66.  
  67. # Convert header to int value
  68. username_length = int(username_header.decode('utf-8').strip())
  69.  
  70. # Receive and decode username
  71. username = client_socket.recv(username_length).decode('utf-8')
  72.  
  73. # Now do the same for message (as we received username, we received whole message, there's no need to check if it has any length)
  74. message_header = client_socket.recv(HEADER_LENGTH)
  75. message_length = int(message_header.decode('utf-8').strip())
  76. message = client_socket.recv(message_length).decode('utf-8')
  77.  
  78. # Print message
  79. print(f'{username} > {message}')
  80.  
  81. except IOError as e:
  82. # This is normal on non blocking connections - when there are no incoming data error is going to be raised
  83. # Some operating systems will indicate that using AGAIN, and some using WOULDBLOCK error code
  84. # We are going to check for both - if one of them - that's expected, means no incoming data, continue as normal
  85. # If we got different error code - something happened
  86. if e.errno != errno.EAGAIN and e.errno != errno.EWOULDBLOCK:
  87. print('Reading error: {}'.format(str(e)))
  88. sys.exit()
  89.  
  90. # We just did not receive anything
  91. continue
  92.  
  93. except Exception as e:
  94. # Any other exception - something happened, exit
  95. print('Reading error: '.format(str(e)))
  96. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement