sms client/server first try

I tried now my first version of server/client system to deliver
sms from one Host to another, I spend now 2 hours, to build a first test Version, and it is
in basic running, will move it to an external git, when I have a first Beta, this is in general running but more or less a case study, have to be finished, then cleaned and secured (auth+crypt):
If you have hints/comments … fell free to post.
On the Server smssrv.py:

#!/usr/bin/python3
# Server program
# Build on 20140201 by Torsten Bade
# Version: 001

import socketserver
import datetime
import configparser

config = configparser.ConfigParser()
config.readfp(open('smspy.cfg'))

HOST = config.get('SERVER', 'HOST')
PORT = config.getint('SERVER', 'PORT')
SMSCNT = config.getint('SERVER', 'SMSCNT')
REPTIME = config.getint('SERVER', 'REPTIME')
hdate = datetime.date.today().strftime('%Y%m%d')  # make date to YYYYmmdd for filename

class MyTCPHandler(socketserver.BaseRequestHandler):
    """
    The RequestHandler class for our server.
    """

    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        #self.data = self.rfile.readline().strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data)
        # just send back the same data, later read logfile and give ack when ok
        self.request.sendall(self.data())
        # loop so that msg gets repeated
        count = 0
        while (count < SMSCNT):
            # build filename for saving, requires a tmp folder in your smspy folder
            filename = "tmp/" + hdate + "sms" + str(count) + ".txt"
            file = open(filename, "wb")
            file.write(self.data)
            file.close
            print(filename)
            count = count + 1

if __name__ == "__main__":
    # Create the server
    server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()

On the Client smsclient.py:

#!/usr/bin/python3
# Echo Client program
# Build on 20140201 by Torsten Bade
# Version: 001

import socket, sys, os, time, glob
import configparser

config = configparser.ConfigParser()
config.readfp(open('smspy.cfg'))

# Get the from Config file
HOST = config.get('SERVER', 'HOST')
PORT = config.getint('SERVER', 'PORT')

# Here come the data for testing
data = "From: Torsten\nTo:49172xxxxx\n\nDies ist ein Test"

# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # Connect to server and send data
    sock.connect((HOST, PORT))
    sock.sendall(bytes(data + "\n", "utf-8"))

    # Receive data from the server and shut down
    received = str(sock.recv(1024), "utf-8")
finally:
    sock.close()

print("Sent:     {}".format(data))
print("Received: {}".format(received))

And smspy.cfg:

[SERVER]
# Arbitrary non-privileged port
PORT=50000
# how much repeats of sms
SMSCNT=3
# time between repeatet sms
REPTIME=10
# Server IP Address, where to listen
HOST=192.168.0.185

[CLIENT]
# not used till now, since I want the server side ready first

2 Gedanken zu „sms client/server first try“

Schreibe einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.