This repository was archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
21 additions
and
13 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
# Import socket module | ||
# Import necessary modules | ||
import socket | ||
import sys | ||
import time | ||
|
||
# Create a socket object | ||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
|
||
# Define the port on which you want to connect | ||
port = 12345 | ||
ip = '127.0.0.1' | ||
filename = 'test.pdf' | ||
# Creating a socket object 'soc_obj'. | ||
# 'AF_NET'-> Ipv4 addressing, 'SOCK_STREAM'-> for TCP protocol. | ||
soc_obj = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
|
||
port, ip = 42420, '127.0.0.1' # Defining port and ip details: | ||
filename = 'test.txt' # test file to send to the server | ||
# connect to the server on local computer | ||
s.connect((ip, port)) | ||
print(f"Connected to {ip}:{port}") | ||
s.send(f"{filename}".encode("utf-8")) | ||
try : | ||
soc_obj.connect((ip, port)) | ||
print(f"Connected to {ip}:{port}.") | ||
except : | ||
print("Failed to establish connection") | ||
sys.exit() | ||
|
||
# sending the test file name,followed by test data | ||
soc_obj.send(f"{filename}".encode("utf-8")) | ||
time.sleep(0.5) # waiting for a short while for server to recieve data | ||
f = open (filename, "rb") | ||
#Sending the data,1KB at a time. | ||
l = f.read(1024) | ||
while (l): | ||
s.send(l) | ||
soc_obj.send(l) | ||
l = f.read(1024) | ||
# close the connection | ||
print(f"{filename} sent") | ||
s.close() | ||
soc_obj.close() |