How to get Tor relay info via python + stem on linux? - python

I am trying to control tor on ubuntu linux using python's stem library as instructed on tor's website. However, when I ran the suggested python code
from stem.control import Controller
with Controller.from_port(port = 9051) as controller:
controller.authenticate() # provide the password here if you set one
bytes_read = controller.get_info("traffic/read")
bytes_written = controller.get_info("traffic/written")
print "My Tor relay has read %s bytes and written %s." % (bytes_read, bytes_written)
I get the error:
Traceback (most recent call last):
File "littleRelay.py", line 5, in module
bytes_read = controller.get_info("traffic/read")
File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 852, in get_info
raise exc
stem.InvalidArguments: GETINFO request contained unrecognized keywords: traffic/read
So how can I get Tor relay info via python+stem on linux?
I think Tor is running fine because I started tor from the terminal and it says
[notice] Tor has successfully opened a circuit. Looks like client functionality is working.
[notice] Bootstrapped 100%: Done.
Furthermore, when I run the above python code, the terminal says
[notice] New control connection opened.
P.S. I have also tried the code on a windows pc and it worked. I'm really puzzled now.

That error indicates that Tor doesn't support the 'GETINFO traffic/read' query. This is odd - that is a feature I added to Tor back in 2011. Perhaps your copy of Tor is very, very out of date?

Problem Solved! Thank you Damian!
I uninstalled Tor on Ubuntu and install Tor again by following the detail guideline here. Now Tor works with the python code.
I'm not sure how exactly the problem arose but I suppose the problem had to do with installing Tor on Ubuntu by naively using
sudo apt-get install tor

Related

CoC server connection error when open a python file

I use Neovim with conquer of completion (CoC) and it works so good in all lenguages except python, when i try to open a python file it shows [coc] server connection lost. I have coc-pyright extension installed.

Use tika with python, runtimeerror: unable to start tika server

I am trying to use the tika package to Parse files. Tika is successfully installed, tika-server-1.18.jar runned with Code in cmd Java -jar tika-server-1.18.jar
My code in the Jupyter is:
Import tika
from tika Import parser
parsed = parser.from_file('')
However, I receive below error:
2018-07-25 10:20:13,325 [MainThread ] [WARNI] Failed to see startup
log message; retrying... 2018-07-25 10:20:18,329 [MainThread ]
[WARNI] Failed to see startup log message; retrying... 2018-07-25
10:20:23,332 [MainThread ] [WARNI] Failed to see startup log
message; retrying... 2018-07-25 10:20:28,340 [MainThread ] [ERROR]
Tika startup log message not received after 3 tries. 2018-07-25
10:20:28,340 [MainThread ] [ERROR] Failed to receive startup
confirmation from startServer.
RuntimeError: Unable to start Tika Server.
According to Apache Tika's site, all new versions of the tika-server.jar will require Java 8.
24 April 2018: Apache Tika Release
Apache Tika 1.18 has been released! This release includes bug fixes (e.g. extraction from grouped shapes in PPT), security fixes and upgrades to dependencies. PLEASE NOTE: The next versions will require Java 8. Please see the CHANGES.txt file for the full list of changes in the release and have a look at the download page for more information on how to obtain Apache Tika 1.18.
Current outdated docs for tika Python library claim that Java 7 is needed, but now Java 8 must be installed. This is because the current version of tika-server.jar is automatically downloaded at runtime if not found in your temp file.
After installing Java 8, my basic test code launched the server and worked without error.
After you import Tika you need to initialize the Java Server
import tika
tika.initVM()
from tika import parser
parsed = parser.from_file('') //file name should be here
Download Java. If you already have a version of Java installed, try updating it to the latest version. The version that works for me is 1.18.
You have not passed an argument (specified a file) in your line:
parsed = parser.from_file('')
Give it a file to chew on e.g.,
parsed = parser.from_file('myfile.txt')
The server didn't start & presumably this no log warning gets triggered - see line 644 in the source at the Github
then another error message tells you it ain't going to play...
I faced similar issue. Tried all steps mentioned here, nothing helped.
How I solved it:
checked the log file of tika and tika-server.
For windows, you can find it inside C:/Users/your_user_name/AppData/Local/Temp/
Found that tika-server log had mentioned port already in use error.
check below log snippet -
INFO: Setting the server's publish address to be http://localhost:9998/
WARNING: FAILED SelectChannelConnector#localhost:9998: java.net.BindException: Address already in use: bind
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source)
at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source)
at org.eclipse.jetty.server.nio.SelectChannelConnector.open(SelectChannelConnector.java:187)
at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:316)
at org.eclipse.jetty.server.nio.SelectChannelConnector.doStart(SelectChannelConnector.java:265)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.Server.doStart(Server.java:293)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.apache.cxf.transport.http_jetty.JettyHTTPServerEngine.addServant(JettyHTTPServerEngine.java:417)
at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.activate(JettyHTTPDestination.java:179)
at org.apache.cxf.transport.AbstractObservable.setMessageObserver(AbstractObservable.java:49)
at org.apache.cxf.binding.AbstractBindingFactory.addListener(AbstractBindingFactory.java:95)
at org.apache.cxf.jaxrs.JAXRSBindingFactory.addListener(JAXRSBindingFactory.java:88)
at org.apache.cxf.endpoint.ServerImpl.start(ServerImpl.java:123)
at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:206)
at org.apache.tika.server.TikaServerCli.main(TikaServerCli.java:213)
This clearly indicated that another process is already running in same port. So I just needed to kill java process running on port 9998 (which I assumed might have been defunct)
Once I killed the process in task manager, I tried rerunning the python script, it worked correctly.
To cross check you can also run the tika-server.jar file present in same path - C:/Users/your_user_name/AppData/Local/Temp/ using below command and check if it fails or runs correctly: java -jar tika-server.jar
Hope this will be helpful to someone in future.
If your are using Ubuntu 20.01 (and 18.04) like me, the solution is to Install Oracle JDK 17. Do the following:
sudo add-apt-repository ppa:linuxuprising/java
sudo apt update
sudo apt install oracle-java17-installer
Type java -version on the terminal. You should see the following print-out:
java version "17.0.1" 2021-10-19 LTS`
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)`
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
tika should then be able to extract text from your pdf in python.
parser.from_file(<your pdf file>)

Can't route requests in a python script through tor without sudo

I'm trying to route requests in a python script through tor.
Here's the code:
#!/usr/bin/env python3
import socket
import socks
from urllib import request
socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 9050)
socket.socket = socks.socksocket
ip = request.urlopen('https://api.ipify.org/').read()
print(ip)
When I try to run it as a user (just "./script.py"), it crashes with the following error:
urllib.error.URLError: <urlopen error Socket error: 0x01: General SOCKS server failure>
But if I run the script with sudo ("sudo ./script.py"), it works as expected and prints a tor IP. How can I get it to work without sudo?
Edit 1: I think tor installation is ok, because it works fine with other languages (for example, I can perform requests from a Go script. Also, I can get my python script to work with tor when passing proxies dict to requests.get() (as suggested in a comment below). This solution is acceptable, but I am still wondering, what's wrong with my script.
Edit 2: I'm running Linux Mint 18.3 64-bit. Python and Python3 are pre-installed. Tor was installed via repository (sudo apt-get install tor). I tried installing PySocks globally (sudo pip3 install PySocks) and only for current user (pip3 install --user PySocks).
Did you try looking at : https://tor.stackexchange.com/questions/7101/general-socks-server-failure-while-using-tor-proxy? This seems to be what you are looking for, and they indicate this can be resolved using an intermediate connection on 127.0.0.2. Take a look!
I have tried your code on my machine (after installing the right packages) and it works as it should. Something should be wrong with the way Tor is installed/run in your system. Feel free to add details about your installation to your question and I'll try to take a look at it.

Python code for Bluetooth throws error after I had to reset the adapter

I was trying out bluetooth programming in python. It was working fine till yesterday. This morning, there was a power outage and for some reason, the bluetooth module got disabled and it could not be turned on. So, I did a sudo hciconfig hci0 reset and then turned it on. From that point onwards, the simplest of the programs are failing to execute. Take this one for example. It gets stuck at advertise_service in bluetooth module and throws the following error (FYI: virtualenv was not a problem here. The systemwide python also does the same thing).
Traceback (most recent call last):
File "bt.py", line 17, in <module>
advertise_service( server_sock, "SampleServer", service_id = uuid, service_classes = [ uuid, SERIAL_PORT_CLASS ], profiles = [ SERIAL_PORT_PROFILE ])
File "/home/machinename/.virtualenvs/py27/local/lib/python2.7/site-packages/bluetooth/bluez.py", line 242, in advertise_service
raise BluetoothError (str (e))
bluetooth.btcommon.BluetoothError: (2, 'No such file or directory')
Sometimes I got a different error when I compiled and reinstalled Bluez driver:
Traceback (most recent call last):
File "build/bdist.linux-x86_64/egg/bluetooth/bluez.py", line 268, in advertise_service
bluetooth.btcommon.BluetoothError: error no advertisable device.
But all of these worked like a charm before in that machine; in fact all of the program works just fine with my other ubuntu (14.04LTS) machine as I write this. I inspected the source code, and traced to a _bluetooth.so file - which is a compiled code, hence I couldn't figure out what to do anymore.
Any pointer will be highly appreciated.
This error is due to incompatibility issues with BlueZ 5 and SDP with bluetoothd
Fix for 15.10 and BlueZ 5
Make sure, running sdptool browse local gives following error:
Failed to connect to SDP server on FF:FF:FF:00:00:00: No such file or directory
As it turns out, the culprit is bluetoothd, the Bluetooth daemon. Using SDP with bluetoothd requires deprecated features for some silly reason, so to fix this, the daemon must be started in compatibility mode with bluetoothd -C (or bluetooth --compat).
Find location of bluetooth.service by:
systemctl status bluetooth.service
Then edit bluetooth.service and look for
ExecStart=/usr/libexec/bluetooth/bluetoothd
Append --compat at the end of this line, save, and then run
service bluetooth start
If all goes well, you should be able to successfully run
sudo sdptool browse local
Finally, reset the adapter:
sudo hciconfig -a hci0 reset
Things should work fine now
Old answer
Just to let people know, I believe the latest BlueZ build was somehow broken in my system. I downloaded, compiled and installed the 5.35 version, and nothing was working. I dialed down to 5.34, still same. I also noticed that the bluetooth adapter was going down automatically 3-4 minutes after enabling it using,
sudo hciconfig hci0 up # hci0 is the bt adapter
I used one usb bluetooth dongle to test. It did not go down automatically like the inbuilt adapter, but the problems persisted. Then I used apt-get to reinstall bluez,
apt-get install --reinstall bluez
and all of a sudden everything came back to normal.
To fix:
bluetooth.btcommon.BluetoothError: (2, 'No such file or directory')
You need to:
sudo nano /lib/systemd/system/bluetooth.service
Change from: ExecStart=/usr/lib/bluetooth/bluetoothd
To: ExecStart=/usr/lib/bluetooth/bluetoothd --compat
sudo systemctl daemon-reload
Again, as sidmeister mentioned,
Make sure, running sdptool browse local gives following error:
Failed to connect to SDP server on FF:FF:FF:00:00:00: No such file or directory
But,for those who are using initd system manager, its hard to find a solution if you want to execute sdp_rfcomm_server/client model and the terminal will keep on showing same error again and again.
So for init.d follow these steps:
Stop bluetooth first
$ /etc/init.d/bluetooth stop
Status check
$ /etc/init.d/bluetooth status
Run bluetooth in compatibility mode(don't forget ampersand,otherwise prompt won't turn up )
$ /usr/libexec/bluetooth/bluetoothd --compat&
start bluetooth again
$ /etc/init.d/bluetooth start
again try sdpbrowse
$ sdptool browse local
Things should work for you now.

Python: Header line to use based on my python installation path

I am using xampp webserver for running my web service. I have installed python in my "D:\Software\Python34" local drive. My apache server is also running from "D:\Software\Xampp" drive only. So in my python file instead of the line,
#!/usr/bin/python
I tried using the line
#!/Software/Python34/python
But this throws error in the browser saying ,
Server error!
The server encountered an internal error and was unable to complete your request.
Error message:
couldn't create child process: 720003: test.py
If you think this is a server error, please contact the webmaster.
Error 500
localhost
Apache/2.4.7 (Win32) OpenSSL/0.9.8y PHP/5.4.25
What is the equivalent line for this in my case?
My code is simply,
#!python3
print ("Hello World!")
Without editing default environment variables in windows, we can just use the following shebang,
#!D:\Software\Python27\python
to run the piece of python code we have from any web service directory.

Categories