-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client_Thread.py
86 lines (70 loc) · 2.26 KB
/
Client_Thread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import socket
from scapy.all import *
SERVER_PORT = 1234
MAX_PAYLOAD = 10 # BYTES
BIT_SPLIT = 4
MAX_PACKETS = 2**4-2
def burstclient(SERVER_IP,secret):
# create a TCP/IP socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# set the IP address and port number of the server
server_address = (SERVER_IP, 1234)
print('Connecting to {server_address}...')
# connect to the server
try:
client_socket.connect(server_address)
except Exception as e:
print("Connection failed:", e)
return
i = 0
# with open("tosend.txt","rb") as f:
# file_bytes = bytearray(f.read())
# bits = ''.join(format(byte, '08b') for byte in file_bytes)
# print(bits)
file_bytes = bytearray(bytes(secret, encoding='utf8'))
bits = ''.join(format(byte, '08b') for byte in file_bytes)
print(bits)
n = len(bits)
print("datalen:", n)
# to make it divisible by BIT_SPLIT
padding = n % BIT_SPLIT
if padding != 0:
bits = "0"*(BIT_SPLIT-padding)+bits
n += BIT_SPLIT-padding
print("modified datalen:", n)
print("modified bits:", bits)
# bitstring = bits
# num = int(bitstring, 2)
# calculate the number of bytes needed to represent the integer
# num_bytes = (len(bitstring) + 7) // 8
# # convert the integer to bytes
# bytes_data = num.to_bytes(num_bytes, byteorder='big')
# print(bytes_data.decode())
try:
message = "0"*MAX_PAYLOAD
enc = message.encode()
j = BIT_SPLIT
g = 0
while i < n:
# send data to the server
numOfPackets = int(bits[i:i+j], 2)+1
# print(numOfPackets, ":", bits[i:i+j])
print(numOfPackets)
g += numOfPackets
for p in range(numOfPackets):
time.sleep(0.3)
client_socket.sendall(enc)
print("No.of packets sent:", g)
time.sleep(7)
i = i+j
print(i)
print("Transfer Successful")
# client_socket.sendall("bye".encode())
# client_socket.close()
# time.sleep(2)
except Exception as e:
client_socket.close()
print(e)
return
if __name__=="__main__":
burstclient('172.16.128.155',"xx")