Skip to content

Instantly share code, notes, and snippets.

@studiawan
Last active March 11, 2021 06:34
Show Gist options
  • Save studiawan/6710819 to your computer and use it in GitHub Desktop.
Save studiawan/6710819 to your computer and use it in GitHub Desktop.
Server always accept client until keyboard interrupt
import socket
import sys
# define server address, create socket, bind, and listen
server_address = ('localhost', 5000)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(server_address)
server_socket.listen(5)
# infinite loop accepting client
try:
while True:
client_socket, client_address = server_socket.accept()
print(client_socket, client_address)
# receive data from client and print
data = client_socket.recv(1024)
print(data)
# close socket client
client_socket.close()
# if user press ctrl + c, close socket client and exit
except KeyboardInterrupt:
server_socket.close()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment