-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPServer.py
38 lines (29 loc) · 980 Bytes
/
TCPServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# TCPServer.py
from socket import *
import sys
args = sys.argv
if len(args) != 2:
print("Usage: python TCPServer.py port")
exit()
port = int(args[1])
# Create welcoming socket using the given port
welcomeSocket = socket(AF_INET, SOCK_STREAM)
welcomeSocket.bind(('', port))
welcomeSocket.listen(1)
print('Listening on port', port, '...')
# While loop to handle arbitrary sequence of clients making requests
while 1:
# Waits for some client to connect and creates new socket
# for connection
connectionSocket, addr = welcomeSocket.accept()
print('Client Made Connection')
# Read input line from socket
clientSentence = connectionSocket.recv(1024)
print('FROM CLIENT:', clientSentence)
# Capitalize the sentence
serverSentence = clientSentence.upper()
# Write output line to socket
connectionSocket.send(serverSentence)
print('TO CLIENT', serverSentence)
# Close the connection socket
connectionSocket.close()