Python and Network chat

+2 Isaiah Rahmany · August 30, 2014
Here's my code for a simple take a turn chat realy over your network on port 5555 you need to give it an ip as an argument when you run it though


client-linux:

import socket
import sys
import os
from thread import *


def noargsgiven():
    print("******************************")
    print("*  No IP given in arguments  *")
    print("******************************")
    sys.exit()

if len(sys.argv) <= 1: noargsgiven()

args = list(sys.argv)
username = ''
host = str(args[1])
port = 5555


def main():
    while True:
        msgfs1 = s.recv(4096)
        msgfs1dc = msgfs1.decode('utf-8')
        msgts = raw_input(msgfs1dc)
        s.send(str.encode(msgts))
        while True:
            msgfs2 = s.recv(4096)
            msgfs2dc = msgfs2.decode('utf-8')
            print(msgfs2dc)
            if not msgfs1 or msgfs2:
                break
    s.close()
        


def name():
    name = s.recv(4096)
    nametext = name.decode('utf-8')
    username = raw_input(nametext)
    s.send(str.encode(username))
    main()

while True:
    s = socket.socket()
    s.connect((host,port))
    print("Connected to server: " + host + ":" + str(port))
    name()

client-windows:

import socket
import sys
import os
from _thread import *

def noargsgiven():
    print("******************************")
    print("*  No IP given in arguments  *")
    print("******************************")
    sys.exit()

if len(sys.argv) <= 1: noargsgiven()

args = list(sys.argv)

username = str(args[1])
host = '10.0.0.10'
port = 5555


def main():
    while True:
        msgfs1 = s.recv(4096)
        print(80 * "-")
        msgfs1dc = msgfs1.decode('utf-8')
        msgts = input(msgfs1dc)
        s.send(str.encode(msgts))
        while True:
            msgfs2 = s.recv(4096)
            print(80 * "-")
            msgfs2dc = msgfs2.decode('utf-8')
            print(msgfs2dc)
            if not msgfs1 or msgfs2:
                break
    s.close()
        


def name():
    name = s.recv(4096)
    print(80 * "-")
    nametext = name.decode('utf-8')
    username = input(nametext)
    s.send(str.encode(username))
    main()

while True:
    s = socket.socket()
    s.connect((host,port))
    print("Connected to server: " + host + ":" + str(port))
    name()



Server-linux:

import socket
import sys
import os
from thread import *

def noargsgiven():
    print("******************************")
    print("*  No IP given in arguments  *")
    print("******************************")
    sys.exit()

if len(sys.argv) <= 1: noargsgiven()

args = list(sys.argv)

username = ''
host = str(args[1])
port = 5555
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)


def loop2(conn):
    while True:
        conn.send(str.encode("Message to Server -> "))
        messagefc = conn.recv(4096)
        message = messagefc.decode('utf-8')
        print("Message from Client -> " + message)
        messagetc = raw_input("Enter message to send to Client -> ")
        messagetc1 = "Message from Server -> " + messagetc + "\n"
        conn.send(str.encode(messagetc1))
        if not messagefc:
            break
    conn.close()



try:
    s.bind((host,port))
except socket.error as e:
    print(str(e))

s.listen(1)
print("Server IP -> " + host + ":" + str(port))
print("waiting for a connection...")
def threadclinet(conn):
    conn.send(str.encode('Welcome, enter a username -> '))
    
    data = conn.recv(4096)
    username = data.decode('utf-8')
    print("Client chosen Username: " + username)
    if not data:
        sys.exit()
    if username != "": start_new_thread(loop2,(conn,))

    

while True:
    conn, addr = s.accept()
    os.system('clear')
    print('Client Connected: ' + addr[0] + ":" + str(addr[1]))

    start_new_thread(threadclinet,(conn,))

Server-Windows:

import socket
import sys
import os
from _thread import *

def noargsgiven():
    print("******************************")
    print("*  No IP given in arguments  *")
    print("******************************")
    sys.exit()

if len(sys.argv) <= 1: noargsgiven()

args = list(sys.argv)

username = ''
host = str(args[1])
port = 5555
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)


def loop2(conn):
    while True:
        conn.send(str.encode("Message to Server -> "))
        messagefc = conn.recv(4096)
        print(80 * "-")
        message = messagefc.decode('utf-8')
        print("Message from Client -> " + message)
        print(80 * "-")
        messagetc = input("Enter message to send to Client -> ")
        messagetc1 = "Message from Server -> " + messagetc + "\n"
        conn.send(str.encode(messagetc1))
        if not messagefc:
            break
    conn.close()



try:
    s.bind((host,port))
except socket.error as e:
    print(str(e))

s.listen(1)
print("Started server on -> " + host + ":" + str(port))
print("waiting for a connection...")
def threadclinet(conn):
    conn.send(str.encode('Welcome, enter a username -> '))
    
    data = conn.recv(4096)
    print(80 * "-")
    username = data.decode('utf-8')
    print("Client chosen Username: " + username)
    if not data:
        sys.exit()
    if username != "": start_new_thread(loop2,(conn,))
    if username == "": start_new_thread(threadclinet,(conn,))

    

while True:
    conn, addr = s.accept()
    os.system('cls')
    print('Client Connected: ' + addr[0] + ":" + str(addr[1]))

    start_new_thread(threadclinet,(conn,))

Post a Reply

Replies

Oldest  Newest  Rating
-1 Scott S · September 8, 2014
Lot's of things in this program and many others on here I haven't seen before. Need to learn to figure it out on my own somehow.
0 George Williams · September 8, 2014
Nice code bro, thanks for sharing!
0 Isaiah Rahmany · September 9, 2014
Thx
  • 1

Python

107,266 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator