How to set rpc timeout in thrift python client? - python

I'm writing python client using thrift, but I can't find any available option to set rpc time out.
My client code goes below:
socket = TSocket.TSocket(address, port)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
server = Client.Client(protocol)
transport.open()

You can use socket.setTimeout() method.
from thrift.transport.THttpClient import THttpClient
socket = THttpClient(server_url)
socket.setTimeout(SERVICE_TIMEOUT_IN_mS)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)

Related

how exactly does gRPC for python handles unix domain sockets

After trying a few things, I got to a point where I can run client-server gRPC connection that is UDS based on the same host. Since I didn't find any detailed documentation for this setup, my main concern is that I don't know if I use gRPC API properly.
Any reference/resource/explanation would be warmly welcomed
sources I looked at:
gRPC python API
gRPC python docs
gRPC server in Python with Unix domain socket (SO)
gRPC connection:
server
import grpc
import my_pb2_grpc
from concurrent import futures
server = None
#...
class Listener(my_pb2_grpc.MyServiceServicer):
#...
def create_connection():
if server is None:
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
my_pb2_grpc.add_MyServiceServicer_to_server(Listener(), server)
server.add_insecure_port("unix:///path/to/uds.sock")
server.start()
def stop_connection():
if server:
server.stop(0)
server = None
client
import grpc
import my_pb2_grpc
channel = None
stub = None
#...
def create_connection():
if channel is None:
channel = grpc.insecure_channel("unix:///path/to/uds.sock")
stub = my_pb2_grpc.MyServiceStub(channel)
def stop_connection():
if channel:
channel.close()
channel = None
stub = None

Implement both HTTP and HTTPS on my simple Python socket server

I want my visitors to be able to use both HTTP and HTTPS. I am using a simple Python webserver created with socket. I followed this guide: Python Simple SSL Socket Server, but it wasn't that helpful because the server would crash if the certificate cannot be trusted in one of the clients. Here is a few lines of code from my webserver that runs the server:
def start(self):
# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket object to the address and port
s.bind((self.host, self.port))
# start listening for connections
s.listen(100)
print("Listening at", s.getsockname())
while True:
# accept any new connection
conn, addr = s.accept()
# read the data sent by the client (1024 bytes)
data = conn.recv(1024).decode()
pieces = data.split("\n")
reqsplit = pieces[0].split(" ");
# send back the data to client
resp = self.handleRequests(pieces[0], pieces);
conn.sendall(resp)
# close the connection
conn.close()
Have another service (something like nginx) handle the https aspect, then configure the service to reverse proxy to your python server

Python sctp module - server side

I have been trying to test SCTP for a network deployment.
I do not have an SCTP server or client and was hoping to be able use pysctp.
I am fairly certain that I have the client side code working.
def sctp_client ():
print("SCTP client")
sock = sctp.sctpsocket_tcp(socket.AF_INET)
#sock.connect(('10.10.10.70',int(20003)))
sock.connect(('10.10.10.41',int(21000)))
print("Sending message")
sock.sctp_send(msg='allowed')
sock.shutdown(0)
sock.close()
Has anybody had luck with using the python sctp module for the server side?
Thank you in Advance!
I know that this topic's a bit dated, but I figured I would respond to it anyway to help out the community.
In a nutshell:
you are using pysctp with the sockets package to create either a client or a server;
you can therefore create your server connection as you normally would with a regular TCP connection.
Here's some code to get you started, it's a bit verbose, but it illustrates a full connection, sending, receiving, and closing the connection.
You can run it on your dev computer and then use a tool like ncat (nmap's implementation of netcat) to connect, i.e.: ncat --sctp localhost 80.
Without further ado, here's the code... HTH:
# Here are the packages that we need for our SCTP server
import socket
import sctp
from sctp import *
import threading
# Let's create a socket:
my_tcp_socket = sctpsocket_tcp(socket.AF_INET)
my_tcp_port = 80
# Here are a couple of parameters for the server
server_ip = "0.0.0.0"
backlog_conns = 3
# Let's set up a connection:
my_tcp_socket.events.clear()
my_tcp_socket.bind((server_ip, my_tcp_port))
my_tcp_socket.listen(backlog_conns)
# Here's a method for handling a connection:
def handle_client(client_socket):
client_socket.send("Howdy! What's your name?\n")
name = client_socket.recv(1024) # This might be a problem for someone with a reaaallly long name.
name = name.strip()
print "His name was Robert Paulson. Er, scratch that. It was {0}.".format(name)
client_socket.send("Thanks for calling, {0}. Bye, now.".format(name))
client_socket.close()
# Now, let's handle an actual connection:
while True:
client, addr = my_tcp_socket.accept()
print "Call from {0}:{1}".format(addr[0], addr[1])
client_handler = threading.Thread(target = handle_client,
args = (client,))
client_handler.start()
Unless you need the special sctp_ functions you don't need an sctp module at all.
Just use protocol 132 as IPPROTO_SCTP (is defined on my python3 socket module but not on my python2 socket module) and you can use the socket,bind,listen,connect,send,recv,sendto,recvfrom,close from the standard socket module.
I'm doing some SCTP C development and I used python to better understand SCTP behavior without the SCTP module.

Detect client is connected to which port

I have the following code and I want to detect in which port client is connected to factory. How can I achieve that?
from twisted.internet import reactor
conn = txredisapi.lazyRedisConnectionPool(reconnect = True)
factory = STSFactory(conn)
factory.clients = []
print "Server started"
reactor.listenTCP(11000,factory)
reactor.listenTCP(11001,factory)
reactor.listenTCP(11002,factory)
reactor.run()
The two addresses of a TCP connection, the client address and the server address, can be retrieved using the transport's getHost and getPeer methods. The "host" address is the address of the local side of the connection. The "peer" address is the address of the other side of the connection. On a server, the host address is also the address of the port which accepted the connection.
You can read about transport methods in the Twisted API documentation.
For example:
class SomeProtocol(Protocol):
def connectionMade(self):
print 'Connection made to', self.transport.getHost(),
print 'from', self.transport.getPeer()

Scala equivalent of python echo server/client example?

All the "server" example in scala use actors, reactors etc...
Can someone show me how to write a dead simple echo server and client, just like the following python example of Server and Client:
# A simple echo server
import socket
host = ''
port = 50000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
while 1:
client, address = s.accept()
data = client.recv(size)
if data:
client.send(data)
client.close()
# A simple echo client
import socket
host = 'localhost'
port = 50000
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
s.send('Hello, world')
data = s.recv(size)
s.close()
print 'Received:', data
You can do following within standard library:
// Simple server
import java.net._
import java.io._
import scala.io._
val server = new ServerSocket(9999)
while (true) {
val s = server.accept()
val in = new BufferedSource(s.getInputStream()).getLines()
val out = new PrintStream(s.getOutputStream())
out.println(in.next())
out.flush()
s.close()
}
// Simple client
import java.net._
import java.io._
import scala.io._
val s = new Socket(InetAddress.getByName("localhost"), 9999)
lazy val in = new BufferedSource(s.getInputStream()).getLines()
val out = new PrintStream(s.getOutputStream())
out.println("Hello, world")
out.flush()
println("Received: " + in.next())
s.close()
If you don't mind using extra libraries, you might like Finagle.
I just wrote a blog post about using Akka IO and Iteratees to create a simple command based socket server.
Maybe it could be of interest.
http://leon.radley.se/2012/08/akka-command-based-socket-server/
You would have to use Java Sockets. I found a nice example of a Scala Socket Server/Client at: http://www.scala-lang.org/node/55
You can use netty java library. Here is an example usage in Scala:
https://github.com/mcroydon/scala-echo-server
Generally you need to use Java Socket API. In this example Java Socket API are used, but the whole server is wrapped in Actor in order to process clients in separate thread and not to block acceptor thread (the same thing you will normally do in Java, but you will use threads directly).
Josh Suereth recently posted an example of an NIO echo server using scalaz Iteratees. Requires the scalaz library

Categories