Skip to content

Commit 0827bad

Browse files
committed
Python Programs
1 parent d3f4e7f commit 0827bad

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import socket
2+
3+
ClientSocket = socket.socket()
4+
host = '127.0.0.1'
5+
port = 1233
6+
7+
print('Waiting for connection')
8+
try:
9+
ClientSocket.connect((host, port))
10+
except socket.error as e:
11+
print(str(e))
12+
13+
Response = ClientSocket.recv(1024)
14+
while True:
15+
Input = input('Say Something: ')
16+
ClientSocket.send(str.encode(Input))
17+
Response = ClientSocket.recv(1024)
18+
print(Response.decode('utf-8'))
19+
20+
ClientSocket.close()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import socket
2+
import os
3+
from _thread import *
4+
5+
ServerSocket = socket.socket()
6+
host = '127.0.0.1'
7+
port = 1233
8+
ThreadCount = 0
9+
try:
10+
ServerSocket.bind((host, port))
11+
except socket.error as e:
12+
print(str(e))
13+
14+
print('Waitiing for a Connection..')
15+
ServerSocket.listen(5)
16+
17+
#Function for handling requests by a thread
18+
def threaded_client(connection):
19+
connection.send(str.encode('Welcome to the Server\n'))
20+
while True:
21+
data = connection.recv(2048)
22+
reply = 'Server Says: ' + data.decode('utf-8')
23+
if not data:
24+
break
25+
connection.sendall(str.encode(reply))
26+
connection.close()
27+
28+
while True:
29+
Client, address = ServerSocket.accept()
30+
print('Connected to: ' + address[0] + ':' + str(address[1]))
31+
start_new_thread(threaded_client, (Client, ))
32+
ThreadCount += 1
33+
print('Thread Number: ' + str(ThreadCount))
34+
ServerSocket.close()

0 commit comments

Comments
 (0)