This is a small lab I have done to see how easy to use python to transfer file over TCP and UDP. File formats including plain text, MS word, pdf, image and vedio.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import sys | |
TCP_IP = "127.0.0.1" | |
FILE_PORT = 5005 | |
DATA_PORT = 5006 | |
buf = 1024 | |
file_name = sys.argv[1] | |
try: | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect((TCP_IP, FILE_PORT)) | |
sock.send(file_name) | |
sock.close() | |
print "Sending %s ..." % file_name | |
f = open(file_name, "rb") | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect((TCP_IP, DATA_PORT)) | |
data = f.read() | |
sock.send(data) | |
finally: | |
sock.close() | |
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
TCP_IP = "127.0.0.1" | |
FILE_PORT = 5005 | |
DATA_PORT = 5006 | |
timeout = 3 | |
buf = 1024 | |
sock_f = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock_f.bind((TCP_IP, FILE_PORT)) | |
sock_f.listen(1) | |
sock_d = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock_d.bind((TCP_IP, DATA_PORT)) | |
sock_d.listen(1) | |
while True: | |
conn, addr = sock_f.accept() | |
data = conn.recv(buf) | |
if data: | |
print "File name:", data | |
file_name = data.strip() | |
f = open(file_name, 'wb') | |
conn, addr = sock_d.accept() | |
while True: | |
data = conn.recv(buf) | |
if not data: | |
break | |
f.write(data) | |
print "%s Finish!" % file_name | |
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
import sys | |
UDP_IP = "127.0.0.1" | |
UDP_PORT = 5005 | |
buf = 1024 | |
file_name = sys.argv[1] | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.sendto(file_name, (UDP_IP, UDP_PORT)) | |
print "Sending %s ..." % file_name | |
f = open(file_name, "r") | |
data = f.read(buf) | |
while(data): | |
if(sock.sendto(data, (UDP_IP, UDP_PORT))): | |
data = f.read(buf) | |
time.sleep(0.02) # Give receiver a bit time to save | |
sock.close() | |
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import select | |
UDP_IP = "127.0.0.1" | |
IN_PORT = 5005 | |
timeout = 3 | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.bind((UDP_IP, IN_PORT)) | |
while True: | |
data, addr = sock.recvfrom(1024) | |
if data: | |
print "File name:", data | |
file_name = data.strip() | |
f = open(file_name, 'wb') | |
while True: | |
ready = select.select([sock], [], [], timeout) | |
if ready[0]: | |
data, addr = sock.recvfrom(1024) | |
f.write(data) | |
else: | |
print "%s Finish!" % file_name | |
f.close() | |
break |
Top comments (0)