Esempio n. 1
0
import socket
import mylogger
import pickle

IPV4 = socket.AF_INET
TCP = socket.SOCK_STREAM
PORT = 1234
#IPADDRESS = 'dbelab04'
IPADDRESS = 'localhost'  # localhost or 127.0.0.1
#logging setup
logger = mylogger.init_logging(name='basic_client', loglevel=mylogger.DEBUG)

s = socket.socket(IPV4, TCP)  # create socket object

try:
    # ans = 1/0
    s.connect((IPADDRESS, PORT))  # attempt connection to server
    logger.debug('Connected to server.')
    while True:
        msg = s.recv(1024)  # buffer size 1024 bytes for incoming message
        if not msg:  #If conn.recv() returns an empty bytes object, b'', then the client closed the connection and the loop is terminated
            break
        msg_pickle = pickle.loads(msg)  # unpickle message
        print msg_pickle
except socket.error as e:
    print "Socket Error: %s" % e
    logger.debug("Socket Error: %s" % e)
except KeyboardInterrupt as e:
    print("KeyboardInterrupt has been caught.")
    logger.debug("Keyboard Error: %s" % e)
except Exception as e:
Esempio n. 2
0
import socket
import mylogger

IPADDRESS = 'dbelab04'
#IPADDRESS = 'localhost'
PORT = 1234

#logging setup
logger = mylogger.init_logging(name='server1', loglevel=mylogger.DEBUG)

while True:
    x = raw_input("Connect to server? 'y' or 'no':\n")
    if x == 'n':
        logger.debug('Exiting.')
        break
    elif x == 'y':
        s = socket.socket()  # create socket object
        logger.debug('Socket created.')
        s.connect((IPADDRESS, PORT))  # connect to server
        line = s.recv(1024)  # receive msg 1024 size
        print 'Received msg:', line  # print receive msg
        s.close()  # close socket connection
        logger.debug('Socket closed.')
        if s is not None:
            del s
Esempio n. 3
0
import platform
import time

HEADERSIZE = 10

#CONSTANTS
IPV4 = socket.AF_INET
TCP = socket.SOCK_STREAM
PORT = 1234

#pickle msg
#d= platform.uname()  # returns tuple of machine information
#msg = pickle.dumps(d)  # pickles tuple

#logging setup
logger = mylogger.init_logging(name='basic_server', loglevel=mylogger.DEBUG)

s = socket.socket(IPV4, TCP)  # create socket object
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
             1)  # allows reuse of address and port

s.bind(
    ('0.0.0.0', PORT
     ))  # Binding to '0.0.0.0' or '' allows connections from any IP address:
s.listen(5)  # queue of 5
logger.debug('Socket is listening.')

while True:
    clientsocket, address = s.accept()  # accept connection from client
    logger.debug('Connection accepted from %s port %s', address, PORT)