I ran a scan with Zenmap on my linux server and found the following ports to be open
So I went over my python script below and ran it
But I was given this output!
How would this be the case?
Thanks in advance.
Your try block covers a lot of functions, and except is catching everything and calling it "port closed." At minimum, it could be any of the following situations:
Failed to create a socket.
Failed to connect to the server.
Connected but the server disconnected before you tried to send.
Connected and sent, but server disconnected before you tried to recv
Nmap would consider either of the second two as "open" because the initial handshake succeeded. Your script considers them "closed" instead.
Related
I have found the correct Bluetooth address of the device I want to connect to. When I run the code below, it prints "Connecting to device..." but then hangs and never prints "Connected" or finishes running. No errors are thrown.
import asyncio
from bleak import BleakClient
address = "24:71:89:cc:09:05" # Replaced with actual bluetooth address
async def main(address):
print("Connecting to device...")
async with BleakClient(address) as client:
print("Connected")
asyncio.run(main(address))
Is this issue related to my PC software/drivers? I am on a Lenovo laptop running Windows 11.
Or, do some devices not respond to simple "connection requests"? The device I am connecting to is a Tesla Model 3, and I know there is a particular set of data I should be sending to authenticate. If this is the case, how do I send data without connecting with a BleakClient in this way?
Update: I should mention that scanning / discovering devices works just fine via Bleak. And, I tried connecting to other devices via Bleak and the same issue occurred. Bluetooth through the Windows Settings app works fine though.
I was also having issues using any bleak commands beyond discover(). I downgraded from 0.14.3 to 0.14.0 and that resolved my problem.
When I looked up "python tesla bluetooth api" I found my way to this site, which documents the API which seems great for what you are doing. That lets you do lots of things like unlocking, opening trunk, etc.
It sounds like the main problem is that you can't connect at all in order to do stuff. The way that site is written glosses over that part, just saying "send it over".
I would ensure that you have the right address, you should use this scanning code from the Bleak doc. Also, make sure that you can use bluetooth normally, like by connecting to a wireless speaker.
Sadly, this error is still unsolved in Bleak, in the project they claim: still waiting for someone to capture bluetooth packets.
Trying to connect, you get either an endless hang with no response or if the device is paired upfront:
Connection error: [WinError -2147483629] The object has been closed
It seems the Library isn't very mature for windows:
https://github.com/hbldh/bleak/issues?q=label%3A%22Backend%3A+WinRT%22+
I'm trying to retrieve data from a PLC (AutomationDirect P2000). I have set up the PLC as the server with their software program (I can also connect to it with their software via Ethernet and use Wireshark to see it is in fact sending UDP packets to my machine at roughly every 200ms). I am trying to set up a very simple Python script to retrieve said data, without bothering to encode it or do anything with it, but my program hangs at the socket.recv(). Whenever I try to run it "Got here" will be printed, but "Now here" will not. From what I've read the fact that it hangs means there's no data to be received, but from my (limited) understanding of what I see on Wireshark this is not the case. I am pretty new to all of this and would appreciate any help.
I have tried using socket.recvfrom(), which produces the same result. I've also tried using socket.bind() instead of socket.connect() but I get a "The requested address is not valid in its context" exception. Additionally, I've tried playing around with various IPs and ports. For example, I've tried using IP = '' instead of the actual IP, and I've tried the source/destination information from Wireshark as what I try to bind or connect to, but nothing thus far has worked.
import socket
IP = '192.168.3.1'
PORT = 9999
BUFFER_SIZE = 4096
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((IP, PORT))
while True:
print("Got here")
data = s.recv(BUFFER_SIZE)
print("Now here")
print(f"Received {data}")
I am expecting to get a print out of the packet in byte format, but instead the program is hanging. If I try socket.bind() instead of socket.connect() I get an error message reading "...line 8, in
s.bind((IP, PORT))
OSError: [WinError 10049] The requested address is not valid in its context"
you can't use bind like this, because the ip address does not belong to your PC.
when you connect to the server, it (the server) doesn't send anything, but you try to get data from the server, so the socket awaits until it gets data, and only then it will continue the execution (this is called a blocking function, since it blocks the execution until it finishes).
The issue was with how I set up the PLC as the server. The UDP data I was seeing on port 9999 wasn't the communications I was thinking it was, and was only the inherent communication between the PLC and the network via its proprietary program. For anyone curious, I am using a P2000 PLC from AutomationDirect and initially I set it up as an EtherNet/IP Adapter following one of their videos, but I had to use the Custom Protocol over Ethernet functionality provided in the "Communications" section.
I've got a python script that basically looks something like this:
#############################
# MAIN LOOP
while True:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client_socket.connect((url, socketnum))
packet = somedata
client_socket.sendall(packet)
except Exception as e:
# an error occurred
logging.error("An error occurred: {}".format(e))
pass
finally:
logging.info("Closing socket...")
client_socket.close()
time.sleep(70)
What I find is that if this script is run before an internet connection is established on the computer (an embedded Linux system), naturally, when the socket tries to connect, I get "Errno -3 Temporary failure in name resolution". However, if the internet connection is then established, the program STILL cannot resolve the hostname - the only way to get it to work is to restart the python script.
Since this system is not one where I can guarantee the presence of an internet connection at all times, is there anyway to get python to realise that the internet connection now exists and that name resolution information is now available?
EDIT: Some further testing shows that this only happens if the python program is started before any successful internet connection is established on the machine after a boot up. If the python program is started AFTER an internet connection has previously been established on the machine (even if it's subsequently been disconnected), the program operates correctly and will successfully connect to the internet after internet connectivity is restored.
So:
Bootup->Python started->Internet connection established = program doesn't work
Bootup->Internet connection established->Internet disconnected->Python started = program works fine.
Try flushing DNS cache in every iteration.
import os
...
while True:
os.popen('nscd -I hosts',"r")
...
or try service nscd restart command instead.
I'm using Bottle server to implement my own server using an implementation not so far away from the simple "hello world" here , my own implementation is (without the routing section of course):
bottleApp =bottle.app()
bottleApp.run(host='0.0.0.0',port=80, debug=true)
My server is keep getting unresponsive all the time and then I get in the Browser: Connection reset by peer, broken pipe errno 32
The logs give me almost exactly the same stack traces such as in question.
Here are my own logs:
What I tried so far, without success:
Wrapping the server run line with try except, something like, shown here the answer of "mhawke".
This stopped the error messages in logs, apparently because I caught them in except clause, but problem is that when catching the exception like that it means that we have been thrown out of the run method context, and I want to catch it in a way it will not cause my server to fall.
I don't know if its possible without touching the inner implementations files of bottle.
Adding this before server run line:
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
As suggested here, but it seems that it didn't had any impact on not getting Broken pipe\connection reset errors and server responsiveness.
I thought of trying also the second answer here, but I don't have any idea where to locate this code in the context of the bottle server.
This sounds like a permissions issue or a firewall.
if you really need to listen on port 80, then you need to run with a privileged account. Also you will probably need to open port 80 for tcp traffic.
I can see your using something that appears to be Posix (Linux/Unix/OSx) If you post what OS you are using I can edit this answer to be more specific as to how to open the firewall and execute privileged commands (probably sudo but who knows).
I'm making a program that needs to recieve a connection hash from a server. When I use:
connhash = s.recv(1024)
I get this error:
[Errno 10054] An existing connection was forcibly closed by the remote host
Is this my fault or the servers fault?
Here is some of the code leading up to s.recv()
stringfmt = u'%(user)s;%(host)s:%(port)d'
string = stringfmt % data
structfmt = '!bh'
encoded = string.encode('utf-16BE')
packetbytes = struct.pack(structfmt, 2, len(encoded))+encoded
s.send(packetbytes)
connhash = s.recv(1024)
I am using Python v 2.7
EDIT: This is for Minecraft just so you know.
It sounds like the remote server doesn't like your connection and cuts you off. This could mean you've made a protocol mistake (i.e., the commands you are sending are incorrect), or you may not have logged in successfully, or your IP may have been banned, or many other similar things.
To debug it, you could try using something like telnet to replicate the connection and see where the error occurs (if it doesn't, then there is something wrong with your implementation; if it does, there is something wrong with your understanding of the protocol, or you are blocked from using the server).
Alternatively, use a packet capture tool like Wireshark to look at what packets are being sent and received, and see if that shows the problem.