-
Notifications
You must be signed in to change notification settings - Fork 1
/
send-file.py
executable file
·37 lines (28 loc) · 1.06 KB
/
send-file.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
#!/usr/bin/env python3
import socket
import sys
def send_file(filename, server="localhost", port=9100, chunk_size=32):
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((server, port))
print("connected to %s:%d" % (server, port))
with open(filename, "rb") as test_file_handle:
data = test_file_handle.read()
chunks = [data[i:i + chunk_size] for i in range(0, len(data), chunk_size)]
print("sending %d chunks" % len(chunks))
for chunk in chunks:
client.send(chunk)
client.close()
if __name__ == "__main__":
if not (2 <= len(sys.argv) <= 4):
print("usage: %s FILE [SERVER PORT]" % sys.argv[0])
print("send a pcl (or any other) file via a socket in 32 bytes chunks")
print()
print("\tFILE a file")
print("\tSERVER defaults to localhost")
print("\tPORT defaults to 9100")
exit(1)
sys.argv += ["localhost", "9100"]
filename = sys.argv[1]
server = sys.argv[2]
port = int(sys.argv[3])
send_file(filename, server, port)