Skip to content

Commit b91067a

Browse files
committed
Make server and client model
0 parents  commit b91067a

7 files changed

+154
-0
lines changed

CLient.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import socket
2+
3+
client_socket = socket.socket()
4+
5+
HOST = '127.0.0.1'
6+
PORT = 1233
7+
8+
try:
9+
client_socket.connect((HOST,PORT))
10+
except:
11+
print("Error occured in connecting ")
12+
13+
Response =client_socket.recv(1024)
14+
print(Response.decode('utf-8'))
15+
while True:
16+
msg = input("Enter the msg")
17+
client_socket.send(str.encode(msg))
18+
responce = client_socket.recv(1024)
19+
print(responce.decode('utf-8'))
20+
21+
client_socket.close()
22+

Mutithreading_servers.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# to solve the problem of concurency
2+
# handling multiple clients using the thread module in python
3+
# for two clients
4+
5+
import socket
6+
from _thread import *
7+
8+
server_socket = socket.socket()
9+
10+
HOST = '127.0.0.1'
11+
PORT = 1233
12+
13+
thread_count = 0
14+
15+
try:
16+
server_socket.bind((HOST, PORT))
17+
except socket.error as err:
18+
print(err)
19+
print("Waiting for a connection")
20+
server_socket.listen(5)
21+
# that handles diff clients
22+
def client_thread(connection):
23+
connection.send(str.encode("Welcome to the server"))
24+
while True:
25+
data = connection.recv(2048)
26+
reply = "Hellow, I am a server "+ data.encode('utf-8')
27+
if not data:
28+
break
29+
connection.sendall(str.encode(reply))
30+
connection.close()
31+
32+
while True:
33+
client, address = server_socket.accept()
34+
print(f"connected to {address[0]} - {str(address[1])}")
35+
start_new_thread(client_thread, (client, ))
36+
thread_count+=1
37+
print(f"No of servers connected are {thread_count}")
38+
server_socket.close()
39+
40+
41+
42+
43+

TCP_client.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import socket
2+
3+
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4+
# takes the tuple of the address and the port no
5+
while True:
6+
client_socket.connect(("172.31.26.173", 8080))
7+
8+
payload = "Hey server"
9+
10+
try:
11+
while True:
12+
client_socket.send(payload.encode("utf-8"))
13+
data = client_socket.recv(2048000)
14+
print(f" Received :", data.decode("utf-8"))
15+
payload = input("Enter your msg-> ")
16+
except:
17+
pass

TCP_server.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# this is a tcp server which will be sending the data from the client and also receiving it after
2+
# connection has been established
3+
4+
import socket
5+
server_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6+
# server has a listen mode to hear for the new_connections to it
7+
server_socket.bind(("192.168.1.7", 800))
8+
# the server has a bind method which binds it to a port and an ip so that it can listen to requests
9+
# on that particular port and addrs.
10+
server_socket.listen(5) #it is a limit of the no of connections
11+
12+
while True:
13+
print("Server waiting for a connection")
14+
client_socket, addr = server_socket.accept()
15+
print("client connected from ", addr)
16+
while True:
17+
data = client_socket.recv(2048000)
18+
if not data: #--->
19+
break
20+
print(f"From other side:-> ",data.decode("utf-8"))
21+
# print(f"Reeived Data form the client in the decoded form ", data.decode("utf-8"))
22+
try:
23+
msg = input("Enter your msg here:-> ")
24+
client_socket.send(bytes(msg, 'utf-8'))
25+
except:
26+
print("Exited by the user")
27+
client_socket.close()
28+
server_socket.close()

UDP_client.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import socket
2+
3+
client_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
4+
5+
msg = "Hellow client here"
6+
client_sock.sendto( msg.encode('utf-8'), ('127.0.0.1', 12345) )
7+
data , addr = client_sock.recvfrom(4096)
8+
print("Server send : ", data.decode('utf-8'))
9+
10+
client_sock.close()

UDP_server.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import socket
2+
3+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
4+
5+
sock.bind(('127.0.0.1', 12345))
6+
7+
while True:
8+
data,addr = sock.recvfrom(4096)
9+
print(str(data))
10+
message = bytes('Hellow i am server here'.encode('utf-8'))
11+
sock.sendto(message, addr )

tcpsockets.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
import socket
3+
import sys
4+
5+
# basically 90% of internet stuff is done using the AF_INET class
6+
try:
7+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8+
except:
9+
print("Fail to create a socket ")
10+
sys.exit()
11+
12+
print("Socket created")
13+
14+
target_host = input("Enter the target host name to connect: ")
15+
target_port = input("Enter the target port: ")
16+
17+
try:
18+
sock.connect((target_host, int(target_port)))
19+
print("Socket connected")
20+
sock.shutdown(2)
21+
except socket.error as err:
22+
print(f"Fail to connect to {target_host} at port no {target_port} due to {err}")
23+

0 commit comments

Comments
 (0)