-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment03_server.py
More file actions
38 lines (35 loc) · 1.13 KB
/
assignment03_server.py
File metadata and controls
38 lines (35 loc) · 1.13 KB
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
#!/usr/bin/python
import socket,sys
def main():
args=sys.argv
script=args[0]
if len(args)<3:
# If there is not enough arguments, exit with error
sys.exit("Not enough arguments!\nUsage: "+script+" <address> <port>")
elif len(args)>3:
print "More than two arguments detected. Only first two arguments parsed."
address=args[1]
# Verify, that port provided is integer
try:
port=int(args[2])
except ValueError:
sys.exit("'"+str(args[2])+"' is not valid port! You must provide integer for port value.")
# create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket to an address and port
sock.bind((address, port))
# start listening for new connections
sock.listen(5)
# Start main loop that runs 'forever'
while True:
# wait for a client using accept()
# accept() returns a client socket and the address from which
# the client connected
(client, addr) = sock.accept()
print "Received a connection from", addr
# read and print whatever the client sends us
print client.recv(1024)
# send "hello world!" back to the client
client.send("hello world!\n")
if __name__ == "__main__":
main()