-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperSimpleSocketServer.py
More file actions
26 lines (26 loc) · 1006 Bytes
/
SuperSimpleSocketServer.py
File metadata and controls
26 lines (26 loc) · 1006 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
#!/usr/bin/python
import socket
import sys
if len(sys.argv) < 3:
print('Usage: %s [hostname] [port number]' % sys.argv[0])
sys.exit(1)
hostname = sys.argv[1]
port = int(sys.argv[2])
#Set up a standard Internet socket. The setsockopt call lets this
#server use the given port even if it was recently used by another
#server (for instance, an earlier incarnation of
#SuperSimpleSocketServer).
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#Bind the socket to a port, and bid it listen for connections.
sock.bind((hostname, port))
sock.listen(1)
print("Waiting for a request.")
#Handle a single request.
request, clientAddress = sock.accept()
print("Received request from", clientAddress)
request.send(bytes('-=SuperSimpleSocketServer 3000=-\n', ‘utf-8’))
request.send(bytes('Go away!\n', ‘utf-8’))
request.shutdown(2) #Stop the client from reading or writing anything.
print("Have handled request, stopping server.")
sock.close()