Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70938

Re: socket client and server in one application?

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'essentially': 0.04; '(so': 0.07; 'initialize': 0.07; 'nicely': 0.07; 'socket': 0.07; 'subject:application': 0.07; 'occasionally': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'both,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'port)': 0.16; 'port,': 0.16; 'threads.': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'app': 0.19; 'input': 0.22; 'cc:addr:python.org': 0.22; 'port.': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'them?': 0.31; 'running': 0.33; 'skip:t 40': 0.33; 'connection': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'idle': 0.36; 'right?': 0.36; 'subject:one': 0.36; 'method': 0.36; 'subject:?': 0.36; 'two': 0.37; 'server': 0.38; 'needed': 0.38; 'whatever': 0.38; 'simply': 0.61; 'simple': 0.61; "you're": 0.61; 'combining': 0.68; 'receive': 0.70; 'serial': 0.72; 'calls,': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=Azlmzm+H8Ciu4OxY0wTfGqZ2owyZDuY27aiMskcUUGg=; b=w4MVu1H+T6rJMN38VE8VWeEhJ2XAa0rJ0D141v5vbECtGDsBySCCAFr6e+m2FADNbM ROEYleJx8YrMBZnqtpj0C6R1ID9L4Sw5q9vG3FHXdRZtU9uDgiON/pYKz91wXN8uz1ks jBQ0MNyYXignCdqlTl+uc3gvdtw3kwH72FkWy/OBssadjJVkdrkTG8flA2ZjIO7an5zj 9iuC1JeVPn1c7/NEDbm3FnDvqwNeEWV3ILhH3JBlCxFkaivkwc2wT9EsxfGlEoSY8c72 GQyyaOY/sb1/V+gONkiFmO3Tt4aRGL1TAnVh0aroKrZ3B87x5Rcssx+cRWndJiJB6/ao wSxQ==
MIME-Version 1.0
X-Received by 10.58.207.74 with SMTP id lu10mr29765969vec.15.1399331122414; Mon, 05 May 2014 16:05:22 -0700 (PDT)
In-Reply-To <69f1b8e6-0b03-4d26-9b89-0d7b3dc43b16@googlegroups.com>
References <2db82378-5bb3-4a5c-b9e0-7ccb29760a06@googlegroups.com> <69f1b8e6-0b03-4d26-9b89-0d7b3dc43b16@googlegroups.com>
Date Tue, 6 May 2014 09:05:22 +1000
Subject Re: socket client and server in one application?
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.9689.1399331130.18130.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1399331130 news.xs4all.nl 2854 [2001:888:2000:d::a6]:45700
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70938

Show key headers only | View raw


On Tue, May 6, 2014 at 8:37 AM,  <chris@freeranger.com> wrote:
> I'm using a dispatch method to receive and occasionally send data through a serial port on a Raspberry Pi.  I think the dispatch method is essentially a threaded approach, right?
>
> Now to receive serial data and send via socket, then occasionally receive some socket based input and send out on the same serial port.  It's combining the client and server socket code into a single app (so I can have a single connection to the serial port) that has me confused.  I don't see any discussion of that anywhere.
>

Threads would be easy. As I understand it, you have two bidirectional
connections, and you're simply linking them? Sounds like a very simple
proxy/tunnel. You don't need to multiplex, so all you need is two
threads: one reading from the socket and writing to the serial port,
and one reading from the serial port and writing to the socket. The
code would look something like this:

serial_port = open(...)
tcp_socket = socket.create_connection(...)
# initialize them both, do whatever setup is needed

def socket_to_serial():
    while True:
        data = tcp_socket.recv(4096)
        serial_port.write(data)

Thread.Thread(target=socket_to_serial).start()

while True:
    data = serial_port.read(4096)
    tcp_socket.send(data)


Two simple loops, running concurrently. Pretty straight-forward as
threads. Both of them will fall idle in their read/recv calls, so
threading works very nicely here.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

socket client and server in one application? chris@freeranger.com - 2014-05-05 08:33 -0700
  Re: socket client and server in one application? Marko Rauhamaa <marko@pacujo.net> - 2014-05-05 18:38 +0300
  Re: socket client and server in one application? CHIN Dihedral <dihedral88888@gmail.com> - 2014-05-05 11:26 -0700
  Re: socket client and server in one application? Grant Edwards <invalid@invalid.invalid> - 2014-05-05 19:47 +0000
  Re: socket client and server in one application? chris@freeranger.com - 2014-05-05 15:37 -0700
    Re: socket client and server in one application? Chris Angelico <rosuav@gmail.com> - 2014-05-06 09:05 +1000
      Re: socket client and server in one application? chris@freeranger.com - 2014-05-15 21:59 -0700

csiph-web