Getting started with Python: implementing Echo Server

Implementation of helloworld with pycharm s

  • Create a project to generate helloworld.py
  • print 'hello world!' one line of code!? Or print "hello world!"
  • $Python hello world.py can output hello world!

    The difference between single quotation mark and double quotation mark

    For a string Let's go, there is a symbol in the middle ", so when defining
    str = 'Let \' s go 'will be added with an escape character; if the double quotation mark str = "Let's go" is used, the string will be clearly expressed.

    For the following expressions, they are all the same

#All execution results are true
print 'Hello python' == "Hello python"
print 'Hello python' == '''Hello python'''
print 'Hello python' == 'Hello'' ''python'
print 'Hello python' == "Hello"" ""python"
print 'Hello python' == '''Hello'''''' ''''''python'''

The implementation of Echo Server

Server side

import socket
import argparse

host = 'localhost'
data_payload = 2048
backlog = 5


def echo(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    server_address = (host, port)

    print 'Starting up echo server on %s port %s' % server_address
    sock.bind(server_address)

    sock.listen(backlog)

    while True:
        print 'Waiting to receive message from client'
        client, address = sock.accept()
        data = client.recv(data_payload)
        if data:
            print 'Receive:%s' % data
            data = data
            client.send(data)
            print 'Send %s bytes back to %s' % (data, address)

        client.close()

//Guess as the main function entry
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Socket Server Example')
    parser.add_argument('--port', action='store', dest='port', type=int, required=True)
    given_args = parser.parse_args()
    port = given_args.port
    print port
    echo(port)


#Usage:python server.py --port=2007

Client

import socket
import argparse

host = 'localhost'


def echo_client(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = (host, port)
    print 'Connecting to %s port %s' % server_address
    sock.connect(server_address)

    try:
        message = 'I \'m a fucking programmer'
        print 'Sending: %s' % message
        sock.sendall(message)

        amount_received = 0
        amount_expected = len(message)

        while amount_received < amount_expected:
            data = sock.recv(16)
            amount_received += len(data)
            print 'Received:%s' % data
    except socket.error, e:
        print 'Socket error: %s' % str(e)
    except Exception, e:
        print 'Other error: %s' % str(e)
    finally:
        print 'Closing connection to the server'
        sock.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Socket Server Example')
    parser.add_argument('--port', action='store', dest='port', type=int, required=True)
    given_args = parser.parse_args()
    port = given_args.port
    print port
    echo_client(port)


# Usage: python client.py --port=2007

Tags: socket Python Pycharm

Posted on Thu, 19 Mar 2020 12:01:28 -0700 by steadyguy