Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6147777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:09:44+00:00 2026-05-23T19:09:44+00:00

I’m trying to procedurally generate images in python to send to a node.js instance,

  • 0

I’m trying to procedurally generate images in python to send to a node.js instance, so I can then deliver those images real-time to a browser through a websocket.

My first test iteration was simply opening a jpeg file in my node.js server, encoding it into base64, and sending it to the browser using socket.io (on port 81). This worked fine every time data was sent.

I then tried opening the same file in python, connecting to a raw TCP node.js server (on port 9090), and sending the file to the node server for it to forward to the browser (on port 81). About half the time, the data node receives seems to be broken, yet the data python reads from the file is fine every time.

What surprises me is that the data channel between node and the browser is fine, yet that between python and node isn’t. Is it the case that websockets provides some error-correction that is not present in a raw TCP socket. If not, what might be causing this error?

Thanks.

JS Code

var io = require('socket.io').listen(81);
var fs = require('fs');
var path = require('path');
var net = require('net');

var mousex = 0;
var mousey = 0;
var imgdata = "";
var oldimgdata = "";
var imagehaschanged = true;

//Serving python
var server = net.createServer(function (stream) {
  stream.setEncoding("base64");
  stream.on("connect", function () {
    console.log("rendering client connected");
  });
  stream.on("data", function (data) {

    //parsed = JSON.parse(data);
    imgdata = data;
    preview = imgdata.substr(0,50);
    console.log(preview);
    if (imgdata!=oldimgdata) {
        imagehaschanged=true;
    }

    console.log("rendering client sent data:");
    posdata = JSON.stringify({'x':mousex ,'y':mousey});
    stream.write(posdata, "ascii", function() {
        console.log("Sent response");
    });

  });
  stream.on("end", function () {
    stream.end();
    console.log("rendering client closed connection");
  });
});
server.listen(9090);

//Serving web
io.sockets.on('connection', function (socket) {
  socket.emit('ready');
  socket.on('position', function(data){
      mousex = data.x;
      mousey = data.y;

      if (imagehaschanged) {
        socket.emit('image', {data: imgdata});
        oldimgdata = imgdata;
        imagehaschanged = false;
      }


      //fs.readFile("1.jpg", processRead);
      //console.log(data.x + ", " + data.y)
  });
});

Python Code

import socket, json,# base64
from time import sleep, time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 9090))

while 1:
    t0 = time()
    f = open("1.jpg")
    data = f.read()
    f.close()
    imgdata = data # base64.b64encode(data)
    print imgdata[:50]
    len = s.send(imgdata)
    response = json.loads(s.recv(4096))
    print "Response is:", response
    sleep(2)
    print "Time taken is: ", time()-t0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:09:44+00:00Added an answer on May 23, 2026 at 7:09 pm

    regardless on how many writes was on client side of tcp socket and what were sizes of messages you generally don’t have control on sizes of data chunks you’ll get on serever’s ‘data’ handler (tcp only guarantees they are in order). You need to frame your data manually (simplest way is to prefix with frame size and buffer incoming data)

    see also this question

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.