-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminal.py
More file actions
50 lines (46 loc) · 1.71 KB
/
Terminal.py
File metadata and controls
50 lines (46 loc) · 1.71 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
39
40
41
42
43
44
45
46
47
48
49
50
import socket
import select
import time
import queue
import threading
ENCODING = 'utf-8'
'''There are two possible input situations. Either the
user wants to give manual input to send to other people,
or the server is sending a message to be printed on the
screen. Select returns from sockets_list, the stream that
is reader for input. So for example, if the server wants
to send a message, then the if condition will hold true
below.If the user wants to send a message, the else
condition will evaluate as true'''
from socket import *
from time import sleep
# Create a socket and connect to the server
serverName = "127.0.0.1" # Use IP address of server
serverPort = 8080
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
# Sign up or login
while 1:
try:
# Listen to response from server and respond to it
response = clientSocket.recv(1024).decode(ENCODING)
if "Error" in response or "Successful" in response:
# Print out any error or congrats messages
print(response)
else:
# Allow for user to input any responses needed
request = input(response)
if "quit" in request:
# Allow for user to quit at anytime
clientSocket.close()
exit()
# Send information to the server
clientSocket.send(request.encode(ENCODING))
sleep(1)
request = ''
except:
# Let user know if there are any issues with processing requests
print("There was an error")
# Close socket if major error occurs (server disconnecting)
clientSocket.close()
exit()