Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

send message from server to client python

import socket
import sys
HOST = ''
PORT = 9000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
try:
    s.bind((HOST, PORT))
except socket.error , msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
print 'Socket bind complete'
s.listen(10)
print 'Socket now listening'
conn, addr = s.accept()
print 'Connecting from: ' + addr[0] + ':' + str(addr[1])
while 1:
    message=raw_input(">")
    s.sendto(message, (addr[0], addr[1]))
    print(s.recv(1024))
Comment

send message from server to client python

# text_send_server.py

import socket
import select
import time

HOST = 'localhost'
PORT = 65439

ACK_TEXT = 'text_received'


def main():
    # instantiate a socket object
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print('socket instantiated')

    # bind the socket
    sock.bind((HOST, PORT))
    print('socket binded')

    # start the socket listening
    sock.listen()
    print('socket now listening')

    # accept the socket response from the client, and get the connection object
    conn, addr = sock.accept()      # Note: execution waits here until the client calls sock.connect()
    print('socket accepted, got connection object')

    myCounter = 0
    while True:
        message = 'message ' + str(myCounter)
        print('sending: ' + message)
        sendTextViaSocket(message, conn)
        myCounter += 1
        time.sleep(1)
    # end while
# end function

def sendTextViaSocket(message, sock):
    # encode the text message
    encodedMessage = bytes(message, 'utf-8')

    # send the data via the socket to the server
    sock.sendall(encodedMessage)

    # receive acknowledgment from the server
    encodedAckText = sock.recv(1024)
    ackText = encodedAckText.decode('utf-8')

    # log if acknowledgment was successful
    if ackText == ACK_TEXT:
        print('server acknowledged reception of text')
    else:
        print('error: server has sent back ' + ackText)
    # end if
# end function

if __name__ == '__main__':
    main()
Comment

PREVIOUS NEXT
Code Example
Python :: python tuple vs list 
Python :: Pyspark Aggregation on multiple columns 
Python :: hashing vs encryption vs encoding 
Python :: python stop while loop after time 
Python :: python acf and pacf code 
Python :: pyton filter 
Python :: python update 
Python :: maxsize in python 
Python :: loop through dataframe column and return unique value 
Python :: numpy 3 dimensional array 
Python :: python cast list to float 
Python :: binary to decimal conversion python 
Python :: matplotlib dateformatter x axis 
Python :: python string vs byte string 
Python :: line length in flake8 
Python :: django sessions 
Python :: import database in python using sqlalchemy 
Python :: pandas get value not equal to 
Python :: python make an object hashable 
Python :: git help 
Python :: xml to excel python 
Python :: flatten image python numpy 
Python :: python find string 
Python :: python append filename to path 
Python :: How To Display A Background Image With Tkinter 
Python :: suppress python 
Python :: tkinter button hide 
Python :: python print 2 decimal places 
Python :: python submit work to redis 
Python :: excute a command using py in cmd 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =