forked from tjclement/esp-dimmer-software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendfile.py
executable file
·46 lines (29 loc) · 1.08 KB
/
sendfile.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
#!/usr/bin/python
import sys
import socket
import ntpath
PORT = 23
def transceive(socket, ip, data):
print data
socket.sendto(data, (ip, PORT))
print socket.recvfrom(1024**2)[0]
def send_file(ip, filename):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
with open(filename) as f:
# Strip paths from the filename, if present
filename = ntpath.basename(filename)
lines = [line.strip('\n') for line in f.readlines()]
transceive(s, ip, 'file.remove("%s")' % filename)
transceive(s, ip, 'file.open("%s", "w+")' % filename)
transceive(s, ip, 'w = file.writeline')
for line in lines:
transceive(s, ip, 'w([[ %s ]])' % line)
transceive(s, ip, 'file.close("%s")' % filename)
if filename.endswith('.lua'):
transceive(s, ip, 'node.compile("%s")' % filename)
if __name__ == '__main__':
if len(sys.argv) != 3:
print 'Usage: sendfile.py ip filename'
print 'Example: sendfile.py 192.168.0.20 init.lua'
exit(0)
send_file(sys.argv[1], sys.argv[2])