SSH tunneling and example Python client-server codes

This document demonstrates how we could run client-server software via SSH. The main purpose of using this technique relies on that on most HPC servers most of the IP ports are nromally blocked except for the SSH port 22.

  1. SSH tunneling settings in PuTTY
    In this case, the server port 8888 is mapped to the local port 7777. The meaing of this is simple: once connected to the server using PuTTY, any data on the server sent/received at port 8888 will be redirect to the port 7777 of the local machine (the computer you are using PuTTY at the moment). As a result, any client software running on the client computer can communicate with the server program via the local port 7777.
  2. Example Python client-server codes using SSHUsage: SSH to the server using PuTTY (with the tunneling settings in Step 1); execute the server code on the server; then run the client code on the local computer.The server code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    </li>
    </ol>
    #!/usr/bin/python

    import sys

    from socket import *

    myHost = '10.72.1.1'  #the server IP

    myPort = 8888            #the server port

    s = socket(AF_INET, SOCK_STREAM)    # create a TCP socket

    s.bind((myHost, myPort))            # bind it to the server port

    s.listen(5)                         # allow 5 simultaneous

    # pending connections

    print "Send 'close' to server to terminate the connection"

    print "Waiting for incoming connection ..."

    while 1:

    # wait for next client to connect

    connection, address = s.accept() # connection is a new socket

    while 1:

    data = connection.recv(1024) # receive up to 1K bytes

    if data:

    print "Data from client %s: %s" % (address,data)

    if data=="close":

    #connection.close()

    print "Disconnected with ", address

    #sys.exit()

    break

    #print connection, address

    connection.send('server feedback -&gt; ' + data)

    else:

    break

    connection.close()              # close socket

    print "Server closed."

    The client code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    import sys

    from socket import *

    serverHost = '127.0.0.1'            # localhost

    serverPort = 7777                   # local port for SSH tunneling

    s = socket(AF_INET, SOCK_STREAM)    # create a TCP socket

    s.connect((serverHost, serverPort)) # connect to server on the port

    s.send('Hello world')               # send the data

    data = s.recv(1024)                 # receive up to 1K bytes

    print data
    Share this:
    Facebook Linkedin Twitter Digg Email

Leave a Reply