Python chat client: the server receives commands along with previously sent messages - python

I'm currently working on a project for a class. It consists in code a simple chat client (protocol given by the teacher) to do (at first) some simple tasks.
My problem is that after I send a mensage on the globlal channel or in other channel that doesn't require the use of a command, and try to send any command, the server replies with an error, saying something like: "msgbeforemsgbeforeCOMMAND" is not a valid command. I just cannot figure it out why this is happening...
(another thing, note that my dictionary is not printing the right why, I dont know why to)
ex:
chat running
import socket, select, string, sys
import threading
import time
def prompt():
sys.stdout.write('<You>: ')
sys.stdout.flush()
tLock = threading.Lock()
shutdown = False
def receber(sock):
while not shutdown:
try:
tLock.acquire()
while True:
data = sock.recv(1024)
if not data:
print ('Disconnected from server\n')
sys.exit()
else:
print ('<server>: %s' % (data.decode()))
sys.stdout.write(data)
except:
pass
finally:
tLock.release()
#Main Function
if __name__ == "__main__":
host = 'mini.alunos.di.uevora.pt'
port = 143
#IP do servidor
try:
busca_ip = socket.gethostbyname( host )
print ('Chat server IP: %s Port: %d \nNow connecting...\n' %(busca_ip, port))
except socket.gaierror:
#Não conseguiu o IP do servidor
print ('Hostname could not be resolved. Exiting.')
sys.exit()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3)
# connectar ao host
try :
s.connect((busca_ip, port))
s.setblocking(0)
except :
print ('Unable to connect to the server.')
sys.exit()
print ('Connected to chat server. You may start chatting\n')
COM_ARG = {'_Comando_': '_Argumentos_',
'NICK': '<nickname> [\<password>]',
'MSG': '<recipient> \<message>',
'ENTER': '<room>',
'LEAVE': '<room> [\<message>]',
'RLIST':'',
'ULIST':''}
for chave, valor, in COM_ARG.items():
print (("%s %s") % (chave,valor))
print ('\n')
comandos = COM_ARG.keys()
#criar thread para o socket
t = threading.Thread(target = receber, args=(s,))
t.start()
while True:
msg = input('<You>: ')
msg = msg.strip()
msg12 = msg.upper()
msg12 = msg12.split()
try:
if msg12[0] in comandos:
msg = msg + '\n'
except:
pass
s.send(msg.encode())
time.sleep(0.25)
btw, sys.stdout.write(data) is doing something there?
Hope you could help me out.

(another thing, note that my dictionary is not printing the right why, I dont know why to)
Dictionary doesn't respect order.
My problem is that after I send a mensage on the globlal channel or in other channel that doesn't require the use of a command, and try to send any command, the server replies with an error, saying something like: "msgbeforemsgbeforeCOMMAND" is not a valid command. I just cannot figure it out why this is happening...
It's not just a problem with the code, the server recives the msgs, and keeps them until a '\n' appears, just then interprets the command. It's a "problem" with the protocol, but the code must be changed.
btw, sys.stdout.write(data) is doing something there?
Supposedly does the samething that print (data.decode()) does, but doesn't work in my case. I'm not sure.

Related

Looking for help in making my socket messenger send instantaneously in Python

Im sure there are easier ways with particular python modules, but for an assignment I need to create a program that can act as a client/server. As of right now I have it working to the point of only being able to send a message if the reciever has responded. I need it to just send and appear on the respective client/server terminal when enter is pressed. Any help would be greatly appreciated!
These are pictures of what happens as of now
https://i.stack.imgur.com/T9CsJ.png
import sys
import socket
import getopt
def usage(script_name):
print('Usage: py' + script_name + '-l' +' <port number>' + '[<server>]')
def sockObj():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
return sock
def serversockConn(serversocket,port):
serversocket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
# bind the serversocket to address
serversocket.bind(('',int(port)))
# enable a server to accept connections
serversocket.listen(5)
# wait for a connection and accept it
sock,addr = serversocket.accept()
return sock
def connToServersock(sock,server, port):
# connect to a serversocket
if server:
sock.connect((server, int(port)))
else:
sock.connect(('localhost', int(port)))
return sock
if __name__ == '__main__':
## get the command line arguments
try:
options, non_option_args = getopt.getopt(sys.argv[1:],'l')
except getopt.GetoptError as err:
print(err)
sys.exit(2)
#check if '-l' is present in command line agrument
serverSide = False
for opt,arg in options:
if opt == "-l":
serverSide = True
# port number
port = non_option_args[0]
# server address
server = None
hostLen = len(non_option_args)
if hostLen == 2:
server = non_option_args[1]
# create a communicator object, and make a connection between server and client
# server
if serverSide:
serversocket = sockObj()
sock = serversockConn(serversocket,port)
# client
else:
sock = sockObj()
sock = connToServersock(sock,server,port)
while True:
## read a message from standard input
message = sys.stdin.readline().encode()
if len(message) != 0:
sock.send(message)
return_msg = sock.recv( 1024 )
if return_msg:
print("Message recieved: " + return_msg.decode())
else:
print("Other side shut down")
else:
try:
sock.shutdown(socket.SHUT_WR)
sock.close()
except:
pass
I think your issue is that there are two places in your event loop where you block:
message = sys.stdin.readline().encode()
Here, you block until the user has pressed return -- during this time, your program is unable to respond to any data received over the network, because it is blocked waiting for data from stdin.
... and:
return_msg = sock.recv( 1024 )
Here, you are waiting for data to be received from the network -- during this time, your program is unable to respond to any data received from stdin, because it is blocked waiting for data from the network.
The behavior you'd ideally like to have is for your program to wait for both stdin and network traffic simultaneously -- i.e. have it block until either the user has pressed return, or some network data has been received, whichever comes first.
The easiest way to achieve that behavior is to use select(); its purpose is to block until at least one of several file descriptors is ready to be acted on. (Note, however, that Windows does not support using select() on stdin, so if your program needs to run under Windows you will probably have to spawn a second thread instead).
To implement the event loop using select(), add import select to the top of your script, then replace your event loop with something like this instead:
while True:
## block here until either sock or sys.stdin has data ready for us
readable, writable, exceptional = select.select([sock, sys.stdin], [], [])
if sys.stdin in readable:
## read a message from standard input
message = sys.stdin.readline().encode()
if len(message) != 0:
sock.send(message)
if sock in readable:
## read a message from the network
try:
return_msg = sock.recv( 1024 )
if (return_msg):
print("Message received: " + return_msg.decode())
else:
print("Other side shut down")
break
except:
print("recv() threw an exception")
break

Regex - how to tell server to ignore unwanted characters

I need your help with my code. I'm building client + server which will execute my raspi camera MJPG-Stream.
import socket
import subprocess
import re
comms_socket = socket.socket()
comms_socket.bind(('', 4244))
comms_socket.listen(20)
reg_vyraz = "Start(\d+)x(\d+)x(\d+)"
try:
while True:
print("Waiting for connection... (Ctrl+C to exit)")
connection, adress = comms_socket.accept()
print("Connected")
try:
while True:
received = connection.recv(4096).decode('UTF-8')
if (len(received) == 0):
break;
m = re.match(reg_vyraz, received)
hodnoty = m.groups()
for cislo in hodnoty:
print (cislo)
print ('Received ', hodnoty, ' from the client')
sys.stdout.write("\n")
except KeyboardInterrupt:
print("Closing connection")
connection.close()
except KeyboardInterrupt:
print("Closing server")
comms_socket.close()
What is it doing? When client sends to the server this: Start640x450x20, server will run the stream with:
Width-640
Height-450
Fps-20
All I need is, to execute this only when I put this "Start640x450x20" into client, when I write something like "gfjlshgslsd" I need server to ignore it, not to turn down.
Thanks for help.
The entire code is correct. You have just missed 1 check.
Just check if m is None or not before calling m.groups()
if m:
hodnoty = m.groups()
for cislo in hodnoty:
print (cislo)

Python simple socket chat User connection and output of message

I am creating a simple chat in python 3 using socket
here are the code
CLIENT
#!/bin/python
import socket
import threading
import time
tLock = threading.Lock()
poweroff = False
def receving(name, sock):
while not poweroff:
try:
tLock.acquire()
while True:
data, addr = sock.recvfrom(1024)
print (str(data))
except:
pass
finally:
tLock.release()
host = '127.0.0.1'
port = 0
server = ('127.0.0.1', 5000)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
s.setblocking(0)
rT = threading.Thread(target=receving, args=("RecvThread", s))
rT.start()
alias = input("Username: ")
time.sleep(0.2)
message = input(alias + ">>> ")
while message != 'q':
if message != "":
s.sendto(str(alias + ": " + message).encode('utf-8'), server)
tLock.acquire()
message = input(alias + ">>> ")
tLock.release()
time.sleep(0.2)
poweroff = True
rT.join()
s.close()
SERVER
#!/bin/python
import socket
import time
hostname = '127.0.0.1'
port = 5000
clients = []
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((hostname, port))
s.setblocking(0)
iQuit = False
print ("Server Started.")
while not iQuit:
try:
data, addr = s.recvfrom(1024)
if addr not in clients:
clients.append(addr)
print (time.ctime(time.time()) + str(addr) + ": :" + str(data))
for client in clients:
s.sendto(data, client)
except:
pass
s.close()
How do i print a string to the server when a user connect?
I have tried to add this string after you have insert the name
s.sendto(str(alias + " Connected").encode('utf-8'), server)
but the output is orrible for me
Another Question:
Why i get something like this when seding a message?
Username: User_A
User_A>>> Hello
User_A>>> How Are you?
b'User:A: Hello'
User_A>>>
b'User_A: How Are you?'
b'User_B: Hi'
Concerning your second question: You are printing binary strings, see here for more information.
Use str(data.decode('utf-8')) instead of str(data) when printing the message on the server or the client.
Concerning the first question: This should work if you send the "Connected" string just after asking for the user name.
The string is decoded the same way as a common message if you include the decode('utf-8') and looks normal to me.
i have to press enter to see if user_B send something to me.
You enforced this behavior by locking out the receiving thread during the input of a message. You have to make up your mind whether you want this or want incoming data to be printed while typing.
You might want to cf. Simultaneous input and output for network based messaging program.

Simple Python Socket Server Not Taking Conditional Statement

I attempted to find an answer for this, but most examples out there are for purely echo base Socket Servers.
Basically I have the following code:
import socket
import sys
from thread import *
HOST = '' # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
#Function for handling connections. This will be used to create threads
def clientthread(conn):
#Sending message to connected client
conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
#infinite loop so that function do not terminate and thread do not end.
while True:
#Receiving from client
data = conn.recv(1024)
if data == "hello":
reply = 'OK...Hello back to you'
else:
reply = '01:OK - ' + data
if not data:
break
conn.sendall(reply)
#came out of loop
conn.close()
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,))
s.close()
Everything works great until I try to use a conditional statement. I am very new to python and I am using this as a way to learn it a little better, but when the following line runs it skips the if each time.
#Receiving from client
data = conn.recv(1024)
if data == "hello":
reply = 'Why hello there!'
else:
reply = '01:OK - ' + data
if not data:
break
conn.sendall(reply)
From the telnet client I am connecting to it just echos everything I send it including the 'hello' I send it rather than the phrase.
I have a feeling that it is something simple, but I am not sure the format of the data variable.
Very close!
Telnet will send whatever EOL delimiter you gave it, along with the text. So if you type "data" and press enter, data is actually something like hello\r\n.
You can effectively ignore this whitespace by doing something like changing
data = conn.recv(1024)
to
data = conn.recv(1024).strip()
And you should be good to go.
EDIT:
As noted in the comments, the network may split up the message into multiple packets. To work around this, you can use use the socket.makefile() method and get a file-like object and then use readline() which will block until a complete line is available. For example, changing clientthread to:
def clientthread(conn):
#Sending message to connected client
conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
sfh = conn.makefile("r+b", bufsize=0)
#infinite loop so that function do not terminate and thread do not end.
while True:
#Receiving from client
data = sfh.readline().strip()
if data == "hello":
reply = 'OK...Hello back to you'
else:
reply = '01:OK - ' + data
if not data:
break
conn.sendall(reply)
#came out of loop
conn.close()
== PYTHON ==
Socket created
Socket bind complete
Socket now listening
Connected with 192.168.1.10:42749
== TELNET ==
$ telnet 192.168.1.106 8888
Trying 192.168.1.106...
Connected to 192.168.1.106.
Escape character is '^]'.
Welcome to the server. Type something and hit enter
hello
OK...Hello back to you

Python Server send data not working

I am currently working on a server in Python, the problem I am facing is the client could not retrieve the sent data from server.
The code of the server is:
import sys
import socket
from threading import Thread
allClients=[]
class Client(Thread):
def __init__(self,clientSocket):
Thread.__init__(self)
self.sockfd = clientSocket #socket client
self.name = ""
self.nickName = ""
def newClientConnect(self):
allClients.append(self.sockfd)
while True:
while True:
try:
rm= self.sockfd.recv(1024)
print rm
try:
self.sockfd.sendall("\n Test text to check send.")
print "Data send successfull"
break
except socket.error, e:
print "Could not send data"
break
except ValueError:
self.sockfd.send("\n Could not connect properly")
def run(self):
self.newClientConnect()
self.sockfd.close()
while True:
buff = self.sockfd.recv(1024)
if buff.strip() == 'quit':
self.sockfd.close()
break # Exit when break
else:
self.sendAll(buff)
#Main
if __name__ == "__main__":
#Server Connection to socket:
IP = '127.0.0.1'
PORT = 80
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
print ("Server Started")
try:
serversocket.bind(('',5000))
except ValueError,e:
print e
serversocket.listen(5)
while True:
(clientSocket, address) = serversocket.accept()
print 'New connection from ', address
ct = Client(clientSocket)
ct.start()
__all__ = ['allClients','Client']
#--
And the client connecting is:
import socket
HOST = '192.168.1.4' # The remote host
PORT = 5000 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
data = s.recv(1024)
s.close()
print 'Received', data#repr(data)
In need of a quick solution....
Thanks,
I tested out your code, and when I commented out
rm= self.sockfd.recv(1024)
print rm
it worked fine. Basically the server stopped there to wait for a message that never came. If it still does not work for you, there might be two problems. Either you have a firewall that blocks the connection somehow, or you have old servers running in the background from previous tries that actually wasn't killed. Check your processes if pythonw.exe or equivalent is running when it shouldn't be, and kill it.
To wait for response:
with s.makefile('rb') as f:
data = f.read() # block until the whole response is read
s.close()
There are multiple issues in your code:
nested while True without break
finally: ..close() is executed before except ValueError: ..send
multiple self.sockfd.close()
etc
Also you should probably use .sendall() instead of .send().
your server code is excepting client send something first,
rm= self.sockfd.recv(1024)
but I don't see any in your code
please try send something in your client code
s.connect((HOST, PORT))
s.send("hello")
Short solution
Add a short sleep after connect.
import time
time.sleep(3)

Categories

Resources