-
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.
- Loading branch information
0 parents
commit b91067a
Showing
7 changed files
with
154 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import socket | ||
|
||
client_socket = socket.socket() | ||
|
||
HOST = '127.0.0.1' | ||
PORT = 1233 | ||
|
||
try: | ||
client_socket.connect((HOST,PORT)) | ||
except: | ||
print("Error occured in connecting ") | ||
|
||
Response =client_socket.recv(1024) | ||
print(Response.decode('utf-8')) | ||
while True: | ||
msg = input("Enter the msg") | ||
client_socket.send(str.encode(msg)) | ||
responce = client_socket.recv(1024) | ||
print(responce.decode('utf-8')) | ||
|
||
client_socket.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# to solve the problem of concurency | ||
# handling multiple clients using the thread module in python | ||
# for two clients | ||
|
||
import socket | ||
from _thread import * | ||
|
||
server_socket = socket.socket() | ||
|
||
HOST = '127.0.0.1' | ||
PORT = 1233 | ||
|
||
thread_count = 0 | ||
|
||
try: | ||
server_socket.bind((HOST, PORT)) | ||
except socket.error as err: | ||
print(err) | ||
print("Waiting for a connection") | ||
server_socket.listen(5) | ||
# that handles diff clients | ||
def client_thread(connection): | ||
connection.send(str.encode("Welcome to the server")) | ||
while True: | ||
data = connection.recv(2048) | ||
reply = "Hellow, I am a server "+ data.encode('utf-8') | ||
if not data: | ||
break | ||
connection.sendall(str.encode(reply)) | ||
connection.close() | ||
|
||
while True: | ||
client, address = server_socket.accept() | ||
print(f"connected to {address[0]} - {str(address[1])}") | ||
start_new_thread(client_thread, (client, )) | ||
thread_count+=1 | ||
print(f"No of servers connected are {thread_count}") | ||
server_socket.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import socket | ||
|
||
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
# takes the tuple of the address and the port no | ||
while True: | ||
client_socket.connect(("172.31.26.173", 8080)) | ||
|
||
payload = "Hey server" | ||
|
||
try: | ||
while True: | ||
client_socket.send(payload.encode("utf-8")) | ||
data = client_socket.recv(2048000) | ||
print(f" Received :", data.decode("utf-8")) | ||
payload = input("Enter your msg-> ") | ||
except: | ||
pass |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# this is a tcp server which will be sending the data from the client and also receiving it after | ||
# connection has been established | ||
|
||
import socket | ||
server_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
# server has a listen mode to hear for the new_connections to it | ||
server_socket.bind(("192.168.1.7", 800)) | ||
# the server has a bind method which binds it to a port and an ip so that it can listen to requests | ||
# on that particular port and addrs. | ||
server_socket.listen(5) #it is a limit of the no of connections | ||
|
||
while True: | ||
print("Server waiting for a connection") | ||
client_socket, addr = server_socket.accept() | ||
print("client connected from ", addr) | ||
while True: | ||
data = client_socket.recv(2048000) | ||
if not data: #---> | ||
break | ||
print(f"From other side:-> ",data.decode("utf-8")) | ||
# print(f"Reeived Data form the client in the decoded form ", data.decode("utf-8")) | ||
try: | ||
msg = input("Enter your msg here:-> ") | ||
client_socket.send(bytes(msg, 'utf-8')) | ||
except: | ||
print("Exited by the user") | ||
client_socket.close() | ||
server_socket.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import socket | ||
|
||
client_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
|
||
msg = "Hellow client here" | ||
client_sock.sendto( msg.encode('utf-8'), ('127.0.0.1', 12345) ) | ||
data , addr = client_sock.recvfrom(4096) | ||
print("Server send : ", data.decode('utf-8')) | ||
|
||
client_sock.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import socket | ||
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
|
||
sock.bind(('127.0.0.1', 12345)) | ||
|
||
while True: | ||
data,addr = sock.recvfrom(4096) | ||
print(str(data)) | ||
message = bytes('Hellow i am server here'.encode('utf-8')) | ||
sock.sendto(message, addr ) |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env python3 | ||
import socket | ||
import sys | ||
|
||
# basically 90% of internet stuff is done using the AF_INET class | ||
try: | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
except: | ||
print("Fail to create a socket ") | ||
sys.exit() | ||
|
||
print("Socket created") | ||
|
||
target_host = input("Enter the target host name to connect: ") | ||
target_port = input("Enter the target port: ") | ||
|
||
try: | ||
sock.connect((target_host, int(target_port))) | ||
print("Socket connected") | ||
sock.shutdown(2) | ||
except socket.error as err: | ||
print(f"Fail to connect to {target_host} at port no {target_port} due to {err}") | ||
|