77 lines
1.9 KiB
Python
Executable File
77 lines
1.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
Author: freezed <freezed@users.noreply.github.com> 2018-02-06
|
|
Version: 0.2
|
|
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
|
|
|
|
roboc
|
|
=====
|
|
|
|
A multiplayer maze game over network
|
|
|
|
This is the client script, see readme.md for more details
|
|
"""
|
|
import socket, signal, select, sys
|
|
|
|
MSG_ARG_ERROR = "Usage: client.py hostname port"
|
|
|
|
if len(sys.argv) < 3:
|
|
print(MSG_ARG_ERROR)
|
|
sys.exit(1)
|
|
|
|
HOST = sys.argv[1]
|
|
PORT = int(sys.argv[2])
|
|
|
|
BUFFER = 1024
|
|
|
|
MSG_SERVER_CONNECTED = "Serveur connecté @{}:{}"
|
|
MSG_CLOSE_CONNECTION = "Connexion vers [{}:{}] fermée"
|
|
|
|
def prompt():
|
|
sys.stdout.write('~')
|
|
sys.stdout.flush()
|
|
|
|
def handler(signum, frame):
|
|
""" Catch <ctrl+c> signal for clean stop"""
|
|
print()
|
|
print(MSG_CLOSE_CONNECTION.format(*(SERVER_CONNECTION.getpeername())))
|
|
SERVER_CONNECTION.send(b"QUIT")
|
|
SERVER_CONNECTION.close()
|
|
sys.exit(0)
|
|
|
|
signal.signal(signal.SIGINT, handler)
|
|
|
|
SERVER_CONNECTION = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
try:
|
|
SERVER_CONNECTION.connect((HOST, PORT))
|
|
except ConnectionRefusedError as except_detail:
|
|
print("ConnectionRefusedError: «{}». Unable to connect".format(except_detail))
|
|
sys.exit()
|
|
|
|
print(MSG_SERVER_CONNECTED.format(HOST, PORT))
|
|
|
|
while 1:
|
|
inputs = [sys.stdin, SERVER_CONNECTION]
|
|
rlist, wlist, elist = select.select(inputs, [], [])
|
|
|
|
for socket in rlist:
|
|
if socket == SERVER_CONNECTION: # incomming message
|
|
data = socket.recv(BUFFER).decode()
|
|
if not data:
|
|
print(MSG_CLOSE_CONNECTION.format(HOST, PORT))
|
|
sys.exit()
|
|
else:
|
|
#print data
|
|
sys.stdout.write(data)
|
|
|
|
prompt()
|
|
|
|
else: # sending message
|
|
msg = sys.stdin.readline().encode()
|
|
SERVER_CONNECTION.send(msg)
|
|
prompt()
|
|
|
|
SERVER_CONNECTION.close()
|