HUB
Sign inSign up

satomic/python-socket

By satomic

•Updated about 8 years ago

for continuiously network connection test

Image
0

235

satomic/python-socket repository overview

⁠github homepage

click me!⁠

⁠how to use

this image was build based on python:2.7-alpine, the key change is add a socket_cs.py in root path /. your running commond show be.

  • expecially the <SERVER_IP> should be interpreted. if you are running the docker in a public cloud env, you shoud set the <SERVER_IP> with INTERNAL_IP, otherwise, a ERROR will pop out.
  • <MODE> support two types server and client docker run -it --net host satomic/python-socket:2.7-alpine python /socket_cs.py <MODE> --host <SERVER_IP> --port <INTER_PORT>

⁠demo

trypically example is like below.

⁠server mode

docker run -it --net host satomic/python-socket:2.7-alpine

⁠client mode

docker run -it satomic/python-socket:2.7-alpine python /socket_cs.py client --host 118.190.156.75


⁠python script content

# coding=utf-8
#!/usr/bin/python

import socket
import commands
import argparse
import traceback
import time
import datetime
import sys

PLATFORM = sys.platform
print("platform is %s" % PLATFORM)

def getstatusoutput(cmd):
    """Return (status, output) of executing cmd in a shell."""
    import os
    if PLATFORM != "win32":
        cmd = '{ ' + cmd + '; } 2>&1'
    pipe = os.popen(cmd, 'r')
    text = pipe.read()
    sts = pipe.close()
    if sts is None: sts = 0
    if text[-1:] == '\n': text = text[:-1]
    return sts, text

def get_host_ip():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip

# parser.add_argument('host', type=str, help="socket server IP") must be set
# parser.add_argument('--host', type=str, help="socket server IP") can be free

parser = argparse.ArgumentParser(description='this script contains socket server & client')
parser.add_argument('--host', type=str, default = get_host_ip(), help="socket server IP")
parser.add_argument('--port', type=int, default = 5000, help="socket server PORT")
parser.add_argument('role', type=str, help="only 'server' and 'client' are supported")
args = parser.parse_args()

HOST = args.host
PORT = args.port
ROLE = args.role

print("%s:%s %s start" % (HOST, PORT, ROLE))

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

if ROLE == "server":
    s.bind((HOST,PORT))
    s.listen(10)         # start TCP listen, only 1 request
    while 1:
        conn,addr = s.accept() 
        print('connected from %s:%s' % addr)
        while 1:
            data = conn.recv(1024)
            if len(data) != 0:
                print("data from client %s:%s len:%s" % (addr, data, len(data)))
            else:
                conn.close()
                break
    print("connection closed by client:%s" % addr)
    
if ROLE == "client":
    s.connect((HOST,PORT))
    while 1:
        # cmd=raw_input("please input cmd:")
        time.sleep(1)
        now_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        print("send time:%s" % now_time)
        s.sendall(now_time)
        # data=s.recv(1024)
        # print(data)
    s.close()

Tag summary
Recent tags
Content type
Image
Digest
Size
21 MB
Last updated
about 8 years ago
docker pull satomic/python-socket:2.7-alpine