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

# Server program
# Build on 20140201 by Torsten Bade
# Version: 001

import socketserver
import datetime
import configparser

config = configparser.ConfigParser()

HOST = config.get('SERVER', 'HOST')
PORT = config.getint('SERVER', 'PORT')
SMSCNT = config.getint('SERVER', 'SMSCNT')
REPTIME = config.getint('SERVER', 'REPTIME')
hdate ='%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.request.recv(1024).strip() = self.rfile.readline().strip()
        print("{} wrote:".format(self.client_address[0]))
        # just send back the same data, later read logfile and give ack when ok
        # 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")
            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

On the Client

# Echo Client program
# Build on 20140201 by Torsten Bade
# Version: 001

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

config = configparser.ConfigParser()

# 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)

    # 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")

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

And smspy.cfg:

# Arbitrary non-privileged port
# how much repeats of sms
# time between repeatet sms
# Server IP Address, where to listen

# 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.