Reading data that is being sent to localhost by another application - python

An application I'm using has the option to enable an API that sends some data when certain events occur to a URL. I configured it to send the data to http://localhost:666/simple/ and used a short program (written by someone else in C#) that takes the data and dumps them to a text file. The author said that you need to run the .exe as administrator to be able to listen to http events and it did indeed work.
I'm trying to achieve the same using python. I took this short example from the requests library and adapted it to the following:
import requests
url = 'http://localhost:666/simple/'
r = requests.get(url, stream=True)
for line in r.iter_lines():
print(line)
I launched command prompt with administrator privileges, but when I try to run this script I get the following error: ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it.
Since the other program is working correctly, I am assuming I am doing something wrong in my code and I'm looking for some help to fix it or an alternative to try out.

Requests are used for, well, making requests from a server, not for being a server.
You may want to look at the docs

use socket to listen data on concrete port

Related

How to ping a java and bedrock Minecraft server's information with Python

I'm trying to find a way to ping a Java+Bedrock Minecraft server using python but I couldn't find the right import to use.
I have recently used mcstatus to get the information but now for some reason I don't get a response from the server anymore.
I also tried using mcipc but I also couldn't get a response from the server.
Does anybody know of a way to ping a java+bedrock server? (there is a chance I used mcipc wrong, but about mcstatus it worked before the server added the bedrock compatibility plugin).
For Java, you do it like this:
serverdata = 'mc.hypixel.net' #you can add port in like this: mc.hypixel.net/25565
data = requests.get(f"https://api.minetools.eu/ping/{serverdata}").json()
try:
print(f"Server is up, and there are {data['players']['online']} players.")
except:
print("Server is offline.")
For Bedrock, I have no idea. Sorry about that.

Pyngrok to getting connecting continuously

I have just started using ngrok, and while using the standard procedure, I can start the tunnel using ./ngrok tcp 22 and see that tunnel open in my dashboard,
But I would like to use pyngrok, and here when I use:
from pyngrok.conf import PyngrokConfig
from pyngrok import ngrok
ngrok.set_auth_token("<NGROK_AUTH_TOKEN>")
pyngrok_config = PyngrokConfig(config_path="/opt/ngrok/ngrok.yml")
ngrok.get_tunnels(pyngrok_config=pyngrok_config)
ssh_url = ngrok.connect()
It connects and generates a tunnel, but I can't see anything open in the dashboard, why?
Maybe because the python script executes and generates URL and then stops and comes out of it, but then how to make it keep running, or how to even start a tunnel using python or even API ? Please suggest the correct script, using python or API?
The thread with the ngrok tunnel will terminate as soon as the Python process terminates. So you are correct, the reason this is happening is because your script is not long lived. The easiest way to accomplish this is by following the example in the documentation.
Another issue is how you're setting the authtoken. Since you're not using the default config_path, you need to set this before setting the authtoken so it gets updated in the correct file (you'd also need to pass it to connect()). There are a couple ways to do this, but the easiest way from the docs is to just update the default config (since that's what will be used if you don't pass a pyngrok_config to any future method calls).
I also see that you're response variable is ssh_url, so you probably want to start a TCP tunnel to a port other than 80 (the default)—perhaps you've configured this in your ngrok.yml, but if not, I've updated the call to connect() to ensure this is the type of tunnel started for you and in case others try to use this same code snippet.
Full disclosure, I am the developer of pyngrok. Here is your code snippet updated with my changes.
import os, time
from pyngrok.conf import PyngrokConfig
from pyngrok import ngrok, conf
conf.get_default().config_path = "/opt/ngrok/ngrok.yml"
ngrok.set_auth_token(os.environ.get("NGROK_AUTH_TOKEN"))
ssh_tunnel = ngrok.connect(22, "tcp")
ngrok_process = ngrok.get_ngrok_process()
try:
# Block until CTRL-C or some other terminating event
ngrok_process.proc.wait()
except KeyboardInterrupt:
print(" Shutting down server.")
ngrok.kill()

Python HTTP PUT on Local Network

I apologise if this is a stupid question, but I can't find any documentation to read on this. Currently, from my Windows machine, I send the following code to a bridge on my local network:
dump = [{
"testdata"
}]
r = requests.put("http://192.0.0.1", dump)
Instead, I wish to send this same code from the bridge to my Windows machine. I have no trouble sending it from the bridge, I am just unsure of how to receive it.
On your Windows machine run a webserver too, on port 80, and listen for PUT requests. You can use Python's built-in HTTP server, there are many examples in the internet. Then parse the data and do whatever you want with them

Handling error response from PYCURL

I relatively new to python, but have been writing some basic scripts for my job to check the status of files on specific servers by ftp. I understand there are better modules for ftp, but due to security restrictions on our work computers we are limited to the basic modules installed on our system which need to handle ftp, sftp, and ftps. Pycurl is the only module we can currently work with.
Now pycurl works successfully at testing the connection by printing the directory and pushing or pulling a file to or from a server via ftp, sftp, fops. Thats not our current issue. The issue is the error response that Pycurl spits out. It doesn't display the ACTUAL error that occurred that you would see from verbose. If we put the wrong remote directory it continue to connect after showing the error in verbose then say something like "Could not access user certificates". WE would like to hand the errors so they display what actually occurred. We saw options such as BUFFERERROR but we haven't figured out how to use them properly. basically, if a sever name is incorrect we would like it to say that.
Does anybody have some experience with pycurl? or know of any debugging script to catch and display the actual errors? I would greatly appreciate it!
you can debug the error by making use of VERBOSE
c = pycurl.Curl()
c.setopt(pycurl.URL,url)
c.setopt(pycurl.HTTPHEADER, ['Authorization: Bearer ' + token)
c.setopt(pycurl.CUSTOMREQUEST, "PUT")
c.setopt(pycurl.POSTFIELDS,data)
c.setopt(pycurl.VERBOSE, 1)
c.perform()
c.close()

What Is The Cause Of This URLOpen Error

My Python code downloads a file from our website. The code fails to download the file on certain clients computers. I cant for the life of me figure out why the file fails to download when the script runs on certain computers but works on others.
The error that occurs on certain computers is:
<urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>
The clients confirm they are connected to the internet and they can successfully download the same file(same url) through a web browser. Its incredibly weird that the script works on some computers and not on others, that they are connected to the internet but cannot download the file and that they can download the file through a browser but not through my script? Maybe the cause it that they are not an admin user?
What can cause this kind of error?
My simple code:
try:
source_buffer = urllib2.urlopen(URL)
source_code = source_buffer.read()
source_buffer.close()
return source_code
except Exception, e:
print e
PS: Do you think this is a proxy error? If it is can you explain what exactly is going wrong? Proxies have always confused me - well I understand when using a proxy all http, https, ftp requests go through a proxy computer (intermediary) before going out to the internet but I dont understand how this error can be caused from a proxy? Whats going wrong? Whats occurring?
It could be proxy, or looking at the error message, it could also be that local/personal firewall settings are blocking the outgoing requests from your application, or responses from the server from reaching your application. Local firewall settings could easily vary between computers, and this might account for the problem.

Categories

Resources