24 lines
704 B
Python
24 lines
704 B
Python
#!/usr/bin/env python3
|
|
|
|
import socket # Import socket module
|
|
import sys
|
|
|
|
s = socket.socket() # Create a socket object
|
|
host = socket.gethostname() # Get local machine name
|
|
port = 12345 # Reserve a port for your service.
|
|
try:
|
|
s.bind((host, port)) # Bind to the port
|
|
|
|
s.listen(5) # Now wait for client connection.
|
|
while True:
|
|
c, addr = s.accept() # Establish connection with client.
|
|
print('Got connection from', addr)
|
|
c.send(b'Thank you for connecting\n')
|
|
c.close() # Close the connection
|
|
except KeyboardInterrupt:
|
|
s.close()
|
|
print("Exiting Server")
|
|
sys.exit()
|
|
finally:
|
|
s.close()
|