tcp sockets in python

jim leahy

Member
i have a lot of programs that need quotes and i'm getting a little tired of writing code for each one
when i get another quote source. all my java programs can share the same source code, but i have
apps in other languages and phone apps. what i did with my etrade quote source was write a quote
server, in java, that uses tcp sockets. my programs then can use a socket protocol to get quotes. i also did
this with an rtd quote server using vb and they both work quite well, although, the vb one is a little slow. i'm
thinking about using python for a new server. has anyone tried using sockets in python? it's quite different
from my usual python programing. it would be a fair amount of work and i don't want to do it if i would
end up with performance or resource problems.

any other suggestions for a different solution?
 

StefanS

New member
i never ran into any problems with python sockets (using the built-in socket and socketserver modules). performance or resource issues would depend on your requirements, obviously. i have stopped using plain tcp sockets a while ago, going instead to messaging protocols/libraries like ZeroMQ (https://zeromq.org/). you get atomic messages, messaging patterns (pubsub with channels, request-reply etc), buffering, indifference as to which side binds or connects, automatic reconnects and more. for my use cases, the added complexity is easily justified. there's a number of alternatives to ZeroMQ as well.
 

garyw

Active member
While not knocking sockets... (haven't considered them for a few years)
I had a unique need in that I wanted realtime and historic data but wanted a common interface. For me, using SQL database(s) was a clean defined, yet extremely flexible interface. (I use MariaDB. There are a number of free SQL database alternatives.)
This allows the programs to be developed independent of the data sources. The database does not need to be on the same machine, and could be in the cloud if your wallet permits. (My wallet does not permit)

The value of this is the "effective API" for accessing data is well defined, documented, and feature rich, as well as being fast.

So, one may have few apps for data collection, but many apps for data consumption, and the two are independent.

This choice is still working great for me.
 

jim leahy

Member
thanks for the replies, guys. i'll look at zeromq, but i would need a compelling reason to change protocols
since a few of my clients already have code to talk to my socket protocol. if python tcp sockets can do the
job i'll probably use that, in the short term.

ultimately i want to do a web quote server but that's a whole different level of effort. i already have
mysql running on a server but all it's doing is storing some intraday data from a cron job running after the close.
i'd be about 1/10 of the way to using mysql as a quote server. when i get to the web server part i'll consider
using a database.
 

gszaboky

New member
Sockets are an API to TCP and UDP protocols. These are the basic building block protocols of the internet. Many other protocols and interfaces rid on top of TCP and UDP. For example HTTP, the protocol of the web. Sockets are the most used and understood API to create a TCP connection to another computer. I am sure Gary's SQL interface is using sockets under the hood. Zeromq rides on top of sockets. Zeromq is good choice for a communications box like a router or switch. It offers a lot of standard patterns, like a publish/subscribe pattern. A lot of engineers designing applications that want a higher-level interface will consider RabbitMq, a slightly different twist from zeromq, that aligns more to applications programming. For SQL solutions, MySql is the most popular SQL open source in the world. It allows centralized and distributed configurations. SQLite is the most popular embedded SQL database in the world. If you have a cell phone, you have several SQLite databases on your phone.
 

jim leahy

Member
to follow up on my sockets question, my quote server has been running for a few weeks
and i haven't run into any issues with sockets or python. i'm not impressed with the
performance but i can't indict python yet; there's a lot of variables. i can say it's noticeably
slower than my java quote server. it's as likely possible it's the quote source api as much as it is
python. if i get bored some day i'll port it to java but for now it's adequate.

i had to add support for websockets because javascript doesn't support regular tcp sockets and i need
to attach to web applications. there's a lot of good examples of websocket implementations on the web,
if anyone wants to code it.
 
Top