-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
30 lines (25 loc) · 687 Bytes
/
server.py
File metadata and controls
30 lines (25 loc) · 687 Bytes
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
#TCP server
import socket, sys
HOST = '127.0.0.1'
PORT = 9999
try:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print("socket created")
except socket.error as err:
print("failed to create a socket")
print("Reason "+ str(err))
sys.exit()
s.bind((HOST,PORT))
print("socket bind to %s" % PORT)
s.listen(5)
print("socket is listening")
while True:
c , addr = s.accept()
print("connection from", addr)
c.send(bytes("Hello, This is from server.",'utf-8'))
while True:
data = c.recv(1024)
if not data or data.decode('utf-8')=='END':
break
print('recieved from client: %s' % data.decode('utf-8'))
c.close()