Thrift TTransportException in python - python

I'm getting a weird error while trying to execute an RPC using thrift on python. I have found similar issues online, but none of them really apply to my situation.
Here is the error I'm getting
No handlers could be found for logger "thrift.transport.TSocket"
Traceback (most recent call last):
File "experiment.py", line 71, in <module>
transport.open()
File "/usr/local/lib/python2.7/dist-packages/thrift/transport/TTransport.py", line 152, in open
return self.__trans.open()
File "/usr/local/lib/python2.7/dist-packages/thrift/transport/TSocket.py", line 113, in open
raise TTransportException(TTransportException.NOT_OPEN, msg)
thrift.transport.TTransport.TTransportException: Could not connect to any of [('192.168.178.44', 9000)]
The following is, I believe, the code which produces it.
TECS_SERVER_IP = "192.168.178.44"
TECS_SERVER_PORT = 9000
transport = TSocket.TSocket(TECS_SERVER_IP, TECS_SERVER_PORT)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = TTSService.Client(protocol)
transport.open()
This happens whenever I try to communicate to another machine, so I tried with the ip "127.0.0.1" and it works. However, using the IP of the localhost "192.168.178.44" which should refer to the same computer also produces the error.
To me it seems like it cannot resolve IP addresses for some reason...
Any ideas on what's causing this and how to fix it?
I'm using Python 2.7.12, thrift 0.9.3 and Ubuntu 16.04, but I also got the error on Windows 10.
This is how my thrift service starts
handler = TTSHandler()
handler.__init__()
transport = TSocket.TServerSocket(host='localhost', port=9000)
processor = TTSService.Processor(handler)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
server.serve()

your server should bind to that address before client could connect to:
TSocket.TServerSocket(host='192.168.178.44', port=9000)
or use host='0.0.0.0' which means bind on all IPv4 addresses on the machine.

Related

WinError 10060 Error when trying to connect to remote server using Python PySFTP module

I am trying to connect to a remote server and list files inside a path using the below code:
import pysftp
myHostname = "myhostname.com"
myPort = <someportnumber>
myUsername = "<valid username>"
myPassword = "<valid password>"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts= cnopts, port=myPort) as sftp:
print("Connection succesfully established ... ")
sftp.chdir('/logs/dev')
# Obtain structure of the remote directory
directory_structure = sftp.listdir_attr()
# Print data
for attr in directory_structure:
print(attr.filename, attr)
But when I am running it, It is unable to establish a connection. It's throwing below exception:
Traceback (most recent call last):
File "c:/Users/611841191/Documents/SFTP File Download/SFTPFileDownload.py", line 11, in <module>
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts= cnopts, port=myPort) as sftp:
File "C:\Users\611841191\AppData\Roaming\Python\Python38\site-packages\pysftp\__init__.py", line 140, in __init__
self._start_transport(host, port)
File "C:\Users\611841191\AppData\Roaming\Python\Python38\site-packages\pysftp\__init__.py", line 176, in _start_transport
self._transport = paramiko.Transport((host, port))
File "C:\Users\611841191\AppData\Roaming\Python\Python38\site-packages\paramiko\transport.py", line 415, in __init__
raise SSHException(
paramiko.ssh_exception.SSHException: Unable to connect to <myhostname.com>: [WinError 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
Can anyone help me out with why this exception is being thrown because I tried with other remote servers and the code seems to be working fine for them? The code is throwing exception for one particular remote server only.
If your code cannot connect somewhere using some protocol (SFTP in this case), the first thing to test is, whether you can connect using the same protocol using any existing GUI/commandline client from the same machine that runs your code.
If that does not work either, you do not have a programming question, but a simple network connectivity problem.
If you need a help resolving the problem, please go to Super User or Server Fault.

Python & Mcpi (minecraft) - connection refused error

I use mcpi: https://github.com/AdventuresInMinecraft/AdventuresInMinecraft-Linux
Starting the local server.
After, run program:
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Hello Minecraft World")
I am facing the below error:
Traceback (most recent call last):
File "/home/home/AdventuresInMinecraft/MyAdventures/HelloMinecraftWorld.py", line 2, in mc = minecraft.Minecraft.create()
File "/home/home/.local/lib/python3.6/site-packages/mcpi/minecraft.py", line 376, in create return Minecraft(Connection(address, port))
File "/home/home/.local/lib/python3.6/site-packages/mcpi/connection.py", line 17, in init self.socket.connect((address, port))
ConnectionRefusedError: [Errno 111] Connection refused
A ConnectionRefusedError means that the address + port combination was unable to be secured for this particular Minecraft server and thus raised an exception. This could be because some other application is already using the port of interest, the port is unavailable because of the OS, or a handful of other networking configuration mishaps.
But perhaps a better series of questions to ask yourself is:
What is the default address and port that minecraft.Minecraft.create() will attempt to launch / listen at?
Do I have access to that server (address + port)?
If I do have access, are there any security issues (AKA Firewall)?
This post has already addressed the root issue of your question, and I hope it gives you a good start at understanding the foundation of your problem.
Notice how their question mentions s.connect((host,port)) and your stack trace has self.socket.connect((address, port)) Looks like the same thing to me!
Some more reading:
- localhost
- check if port is in use
I encountered the same issue. I looked into the code of mcpi and found that the default port is 4711. However, a Minecraft Server's default port is 25565. All you need to do is add 2 parameters on the create() function. Code(Python):
mc = minecraft.Minecraft.create(address="127.0.0.1", port=25565)
btw change "address" in the code to the host of the server (only if you modified the "server.properties" file).
Also, ConnectionRefusedError doesn't mean that it's not secured, I believe it means that either the server is not online, it doesn't exist, or the server refused it for some reason.
EDIT:
Oops sorry I just found out that mcpi actually connects to the RaspberryJam plugin which is hosted on another IP and port. The plugin runs on port 4711. So mcpi has the right port.
So check if you have the RaspberryJam plugin installed. If not, download it from
https://www.spigotmc.org/resources/raspberryjuice.22724/
And put the .jar file inside the plugins folder in your server directory.

Python socket - An invalid argument was supplied

I have a problem with a python server I am creating. It works on my home machine, but when I've tried to run it on a different machine it does not work. When compiled using pyinstaller, the window immideatly closes, and when ran as a raw python file (python 2.7.10 is installed on both my home machine and the machine it is not working on) it throws the error:
Traceback (most recent call last):
File "fileModifyServer.py", line 136, in <module>
startServer()
File "fileModifyServer.py", line 11, in startServer
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
File "N:\Python27\lib\socket.py", line 191, in __init__
_sock = _realsocket(family, type, proto)
socket.error: [Errno 10022] An invalid argument was supplied
My code it is referencing to is as follows:
import socket
def startServer():
global serversocket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((socket.gethostname(), 8010))
serversocket.listen(5)
print "Server started"
The traceback you have is strange. It indicates a line when attempting to instantiate the socket, which would indicate a problem with your python installation or network stack. It also indicates that error occurred on line 11, but in your code the line in question appears on line 6. I'm not sure how it happened here, but I know this can happen if you edit files while your program is running and then it crashes. The traceback simply prints out the line number from the file in question that caused the error, and the file source doesn't appear to be read until the error occurs; Therefore the traceback will reflect the line in the modified file, which isn't the line that was present when the program was compiled, and thus is not the line that actually caused the problem.
Without looking at the traceback, I do see an error with your code. You are attempting to bind your server to an invalid interface. The hostname returned by socket.gethostname is not an interface. From the documentation:
If you want to know the current machine’s IP address, you may want to use gethostbyname(gethostname()).
This operation assumes that there is a valid address-to-host mapping for the host, and the assumption does not always hold.
# for example
local_ip_address = socket.gethostbyname(socket.gethostname())
Which will return a string representation of your local ip address. Unfortunately, that would still throw an error, as it is not an interface that you can bind to.
Some interfaces that you can bind to include "0.0.0.0", which means all available interfaces, and "localhost", which means "local" connections only, so no external network traffic allowed.

How to send a HTTPS packet in python?

I want to send a HTTPS packed In python. For example I want to send something to Google server. I wrote the following program :
import socket
import ssl
# SET VARIABLES
packet, reply = "<packet>SOME_DATA</packet>", ""
HOST, PORT = 'www.google.com', 443
# CREATE SOCKET
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
# WRAP SOCKET
wrappedSocket = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1, ciphers="ADH-AES256-SHA")
# CONNECT AND PRINT REPLY
wrappedSocket.connect((HOST, PORT))
wrappedSocket.send(packet)
print wrappedSocket.recv(1280)
# CLOSE SOCKET CONNECTION
wrappedSocket.close()
But when I run it, I receive the following error :
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "C:/Users/Desktop/PySSL.py", line 16, in <module>
wrappedSocket.connect((HOST, PORT))
File "C:\Python27\lib\ssl.py", line 297, in connect
self.do_handshake()
File "C:\Python27\lib\ssl.py", line 281, in do_handshake
self._sslobj.do_handshake()
SSLError: [Errno 1] _ssl.c:499: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure
>>>
What shall I do to handle it?
Update:
I checked Google encryption protocol already and you can see the result in the following picture :
But I don't have any idea how and where I must set this parameters in the program.
Update:
Note that I want to extract the used public key in the communication. So please suggest me a way that it make extracting the public key possible. I also want to set or see the used communication and encryption protocols.
Update2:
Although I wanted to set the cipher and ssl_version manually, but based on the answers, I tried to remove this to parameters from my function call and retry to run the program. well, the error changed:
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "C:/Users/Desktop/PySSL2.py", line 17, in <module>
wrappedSocket.send(packet)
File "D:\Python34\lib\ssl.py", line 679, in send
v = self._sslobj.write(data)
TypeError: 'str' does not support the buffer interface
>>>
wrappedSocket = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1, ciphers="ADH-AES256-SHA")
You are trying to use ADH, i.e. a cipher with anonymous authentication. This is a cipher which does not use certificates for authentication and is thus usually only supported by misconfigured servers.
Just omit setting ssl_version and ciphers from your function call. In this case it will offer the server the best protocol version it can and a number of ciphers and the server will pick from these offers what it supports.
This should then make the SSL handshake possible. But since it does not look like your are really trying to talk HTTP on the socket you will probably run into further problems after the connection got established.
Why not using requests?
import requests
requests.get('https://www.google.com', port=443)
That way you will be bound to HTTP
Since it looks like you're trying to make simple http requests, you could use the requests library instead, which handles a lot of the stuff for you.
If you don't have it: pip install requests.
import requests
resp = requests.get("http://www.google.com")
print resp.text

Python Multiprocessing: socket error timeout while remote connection to a Manager

newcomer and first ever question here.
I am using the multiprocessing module of Python which is currently creating a Manager and a couple (45) processes on my localhost.
My Manager is set up as following:
manager = QueueManager(address=('', 50000), authkey='abracadabra')
manager.get_server().serve_forever()
I want also to create some other client processes remotely on another computer. So, let's say my IP is a.b.c.d, the Manager of the client in the remote computer is set up as following:
manager = QueueManager(address=('a.b.c.d', 50000), authkey='abracadabra')
manager.connect()
(yes, it's copy-pasted from the documentation).
However, I run the server and all 45 processes in localhost are fine, then I run the remote client and I get this:
Traceback (most recent call last):
File "govmap-parallel-crawler-client.py", line 144, in <module>
manager.connect()
File "/usr/lib/python2.6/multiprocessing/managers.py", line 474, in connect
conn = Client(self._address, authkey=self._authkey)
File "/usr/lib/python2.6/multiprocessing/connection.py", line 134, in Client
c = SocketClient(address)
File "/usr/lib/python2.6/multiprocessing/connection.py", line 252, in SocketClient
s.connect(address)
File "<string>", line 1, in connect
socket.error: [Errno 110] Connection timed out
Both computers can ping and ssh each other without problems.
My guess: there is one (or two!) firewall in between making the connection impossible. Is this correct?
If yes: is there a way to use a safe known port in order to avoid the firewall or maybe a more polite solution?
If no: what is happening?
Thanks!!
Use an ssh tunnel for interconnect? E.g on client:
ssh a.b.c.d -L12345:localhost:50000
If the client connects to localhost port 12345, it should be tunnelled to a.b.c.d port 50000.
EDIT: Of course, using an SSH tunnel might not be the best solution in a production environment, but at least it lets you eliminate other issues.
As defined by your snippet the server listens only on localhost (127.0.0.1) and not (a.b.c.d) thus you cannot connect from remote client.
To do so use:
manager = QueueManager(address=('a.b.c.d', 50000), authkey='abracadabra')
manager.get_server().serve_forever()

Categories