Python: Print data while waiting for input
Question: Writing a chat program and am looking for the ability for it to print messages
that are being received when they are received while the user can enter a
message to the other person. Currently the program sends message to other
computer then receives the other computer's message and then repeats.

    
    
    import sys
    import socket as so
    import platform
    from socket import *
    import string
    import base64
    import os
    import random
    
    dmi = 0
    lmi = 0 
    host = ""
    checkcode = ''.join(random.choice('0123456789QWERTYUIOPLKJHGFDSAZXCVBNMasdfgjhklpoiuytrewqmnzbxvc') for i in range(16))
    BLOCK_SIZE = 32
    hostl = raw_input("> Input IP of computer message will be sent to: ")
    port = 13000
    buf = 1024
    addr = (host, port)
    addrl = (hostl, port)
    UDPSock = socket(AF_INET, SOCK_DGRAM)
    UDPSock.bind(addr)
    addr = (host, port)
    addrl = (hostl, port)
    while dmi == 0:
        datal = raw_input("Enter message to send or type 'exit': ")
        UDPSock.sendto(datal, addrl)
        if datal == "exit":
            UDPSock.close()
            dmi +=1
        (data, addr) = UDPSock.recvfrom(buf)
        print "Received message: "+ data
    print "Done!"
    

Don't ask about the un-needed libraries that I have imported, this is a test
script.


Answer: You need to learn how to use multiple threads
(<http://www.tutorialspoint.com/python/python_multithreading.htm>). One thread
waits for user input, while the other receives the messages and prints them.

