LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-14-2018, 10:41 AM   #1
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Rep: Reputation: Disabled
Thumbs up How to send data from client python tool to main and receive data back


Hi All,

Ive got a main python tool which reads syslog and generates a list of servers it see's (this is a test tool to get the functionality correct).

The client script sends a query on a sock and the main prints the contents of its list however i want to send the contents back to the client and read there rather than being output on the main tool.

Code is below


MAIN TOOL

Code:
#!/usr/bin/python

import sys
import time
import re
import threading
import socket
import errno

def handle():
  serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  PORT = 19999
  HOST = '127.0.0.1'
  MAX_LENGTH = 4096
  try:
     serversocket.bind((HOST, PORT))
  except socket.error, v:
     errorcode=v[0]
     print "Error found during startup [ %s ] " % (errorcode)
     sys.exit(0)
  serversocket.listen(10)
  (clientsocket, address) = serversocket.accept()
  while True:
    buf = clientsocket.recv(MAX_LENGTH)
    if buf == '': return #client terminated connection
    elif buf == "dump":
        for item in serverList:
                print item
    else:
        print "Unrecognised command entered"
        print buf

def follow(thefile):
    thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

handlethread = threading.Thread(target=handle)
handlethread.daemon = True
handlethread.start()

serverList = []

print "hello there"
logfile = open("/var/log/messages","r")
loglines = follow(logfile)
for line in loglines:
        servername=line.split(' ')[3]
        if servername not in serverList:
                serverList.append(servername)
                print "added %s to list of servers" % (servername)

CLIENT TOOL

Code:
#!/usr/bin/python
import socket
import sys


HOST = '127.0.0.1'
PORT = 19999
s = socket.socket()
s.connect((HOST, PORT))

while 1:
    msg = raw_input("Command To Send: ")
    if msg == "close":
       s.close()
       sys.exit(0)
    s.send(msg)

Any help in getting the main tool to send the requested back and read would be most appreciated.
 
Old 08-14-2018, 10:58 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply

Tags
clientserver, python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Receive/send data from drone to laptop on ground Heartjc Linux - Newbie 2 03-27-2016 07:45 AM
How do I send and receive data through a TUN interface? jayadhanesh Linux - Networking 4 02-26-2009 01:49 AM
Not able to send/receive the data packets through my own Ethernet driver b/w 2 pc jagadisha_bs Linux - Newbie 5 02-26-2009 12:31 AM
Modem Data Send/Receive Problem mofargel Linux - Hardware 1 10-16-2007 08:42 PM
can i send or receive data in compress form to internet alvi2 Linux - Networking 1 03-05-2005 07:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration