2020-06-02 14:15:03 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2020-01-28 22:15:42 +01:00
|
|
|
import sys
|
2020-02-06 23:27:29 +01:00
|
|
|
import os
|
2020-06-02 14:15:03 +02:00
|
|
|
import util
|
2020-01-28 22:15:42 +01:00
|
|
|
|
|
|
|
if len(sys.argv) < 2:
|
2020-02-06 21:17:45 +01:00
|
|
|
print("Usage: %s <file> [port]" % sys.argv[0])
|
2020-01-28 22:15:42 +01:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
# Create a TCP/IP socket
|
|
|
|
FILENAME = sys.argv[1]
|
|
|
|
|
2020-06-02 14:15:03 +02:00
|
|
|
# Bind the socket to the port or choose a random one
|
2022-12-09 14:54:06 +01:00
|
|
|
address = util.get_address()
|
2020-06-02 14:15:03 +02:00
|
|
|
port = None if len(sys.argv) < 3 else int(sys.argv[2])
|
|
|
|
sock = util.openServer(address, port)
|
|
|
|
if not sock:
|
|
|
|
exit(1)
|
2020-01-28 22:15:42 +01:00
|
|
|
|
2020-02-06 21:17:45 +01:00
|
|
|
print("Now listening, download file using:")
|
2020-06-02 14:15:03 +02:00
|
|
|
print('nc %s %d > %s' % (address, sock.getsockname()[1], os.path.basename(FILENAME)))
|
2020-02-06 21:17:45 +01:00
|
|
|
print()
|
2020-01-28 22:15:42 +01:00
|
|
|
|
|
|
|
while True:
|
|
|
|
# Wait for a connection
|
|
|
|
print('waiting for a connection')
|
|
|
|
connection, client_address = sock.accept()
|
|
|
|
|
|
|
|
try:
|
|
|
|
print('connection from', client_address)
|
|
|
|
|
|
|
|
with open(FILENAME, "rb") as f:
|
|
|
|
content = f.read()
|
|
|
|
connection.sendall(content)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
# Clean up the connection
|
|
|
|
connection.close()
|