{"id":28028841,"url":"https://github.com/m-taghizadeh/pychat","last_synced_at":"2026-08-03T21:31:01.971Z","repository":{"id":104572118,"uuid":"267654760","full_name":"M-Taghizadeh/PyChat","owner":"M-Taghizadeh","description":"PyChat is a massanger that implemented with python (socket programming)","archived":false,"fork":false,"pushed_at":"2020-06-02T15:51:38.000Z","size":968,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-05T03:07:17.716Z","etag":null,"topics":["messanger","pychat","socket-programming","threading"],"latest_commit_sha":null,"homepage":"http://m-taghizadeh.ir","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/M-Taghizadeh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-05-28T17:36:08.000Z","updated_at":"2020-10-28T10:11:05.000Z","dependencies_parsed_at":"2023-03-13T14:54:20.866Z","dependency_job_id":null,"html_url":"https://github.com/M-Taghizadeh/PyChat","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/M-Taghizadeh/PyChat","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M-Taghizadeh%2FPyChat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M-Taghizadeh%2FPyChat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M-Taghizadeh%2FPyChat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M-Taghizadeh%2FPyChat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/M-Taghizadeh","download_url":"https://codeload.github.com/M-Taghizadeh/PyChat/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M-Taghizadeh%2FPyChat/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":36253066,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-07-20T02:08:10.276Z","status":"online","status_checked_at":"2026-08-03T02:00:06.975Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["messanger","pychat","socket-programming","threading"],"created_at":"2025-05-11T07:31:52.416Z","updated_at":"2026-08-03T21:31:01.960Z","avatar_url":"https://github.com/M-Taghizadeh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **PyChat** Massanger\n\n\u003cp\u003ePyChat is a massanger that developed with python and socket library.\u003c/p\u003e\n\nAuthor: **M-Taghizadeh**\u003cbr\u003e \nhttp://m-taghizadeh.ir\n\n\n# Documentation\n- [What is Socket?](#Socket)\n- [How TCP work?](#How-TCP-work)\n- [How UDP work?](#How-UDP-work)\n- [Server implementation in python](#Server-implementation-in-python)\n- [Client implementation in python](#Client-implementation-in-python)\n- [Threading](#Threading)\n- [PyChat CMD implementation with tcp in python](#PyChat-cmd-implementation-with-tcp-in-python)\n- [PyChat GUI implementation with tcp in python](#PyChat-gui-implementation-with-tcp-in-python)\n- [Dependencies](#Dependencies)\n\n\n# Socket \n\nSockets allow communication between clients and server or two different processes on the same or different machines. To be more precise, it's a way to talk to other computers using standard Unix file descriptors. In Unix, every I/O action is done by writing or reading a file descriptor. A file descriptor is just an integer associated with an open file and it can be a network connection, a text file, a terminal, or something else.\n\nTo a programmer, a socket looks and behaves much like a low-level file descriptor. This is because commands such as read() and write() work with sockets in the same way they do with files and pipes.\n\nSockets were first introduced in 2.1BSD and subsequently refined into their current form with 4.2BSD. The sockets feature is now available with most current UNIX system releases.\n\n![socket-Programming](docs/sockets.png)\n\n### Where is Socket Used?\n\nA Unix Socket is used in a client-server application framework. A server is a process that performs some functions on request from a client. Most of the application-level protocols like FTP, SMTP, and POP3 make use of sockets to establish connection between client and server and then for exchanging data.\n\n### Socket Types\n\nThere are four types of sockets available to the users. The first two are most commonly used and the last two are rarely used.\nProcesses are presumed to communicate only between sockets of the same type but there is no restriction that prevents communication between sockets of different types.\n\n- **Stream** Sockets** − Delivery in a networked environment is guaranteed. If you send through the stream socket three items \"A, B, C\", they will arrive in the same order − \"A, B, C\". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do not have any boundaries.\n\n- **Datagram Sockets** − Delivery in a networked environment is not guaranteed. They're connectionless because you don't need to have an open connection as in Stream Sockets − you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol).\n\n- **Raw Sockets** − These provide users access to the underlying communication protocols, which support socket abstractions. These sockets are normally datagram oriented, though their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not intended for the general user; they have been provided mainly for those interested in developing new communication protocols, or for gaining access to some of the more cryptic facilities of an existing protocol.\n\n- **Sequenced** Packet Sockets − They are similar to a stream socket, with the exception that record boundaries are preserved. This interface is provided only as a part of the Network Systems (NS) socket abstraction, and is very important in most serious NS applications. Sequenced-packet sockets allow the user to manipulate the Sequence Packet Protocol (SPP) or Internet Datagram Protocol (IDP) headers on a packet or a group of packets, either by writing a prototype header along with whatever data is to be sent, or by specifying a default header to be used with all outgoing data, and allows the user to receive the headers on incoming packets.\n\n\n# How TCP work\n\n![tcp-vs-udp](docs/tcp-udp.svg)\n\nA TCP connection is established with the help of three-way handshake. It is a process of initiating and acknowledging a connection. Once the connection is established, data transfer begins, and when the transmission process is finished, the connection is terminated by the closing of an established virtual circuit.\n\n\n# How UDP work\n\nUDP uses a simple transmission method without implied hand-shaking dialogues for ordering, reliability, or data integrity. UDP also assumes that error checking and correction is not important or performed in the application, to avoid the overhead of such processing at the network interface level. It is also compatible with packet broadcasts and multicasting\n\n\n# Server implementation in python\n\n![server-client](docs/server-client.png)\n\n```python\nimport socket\n\n# 1-Define Server:\n# IPv4: AF_INET, IPv6: AF_INET6\n# TCP: SOCK_STREAM, UDP: SOCK_DGRAM\nserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\n# 2-Binding:\nserver_ip = ('localhost', 1234) # ip and port\nserver.bind(server_ip)\n\n# 3-Listening\nserver.listen(2) # 2 client cant connect to this server\n\n# 4-Connection\ncon, addr = server.accept()\n\n# 5-Send Data (Bytes Stream)\n# con.send(b\"This is a message.\")\nwhile True:\n    send_data = str.encode(input(\"\u003e\u003e \"))\n    con.send(\"response: \" + send_data)\n```\n\n\n# Client implementation in python\n\n```python\nimport socket\n\n# 1- Define Client \nclient = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\n# 2- Connect to server\nserver_ip = ('localhost', 1234) \nclient.connect(server_ip)\n\n# 3-Receive Data from server(GET Response) and print it.\n# data = client.recv(1024) # receives blocks of size 1024 from the server\nwhile True:\n    data = client.recv(1024)\n    print(data.decode())\n```\n\n\n# Threading\n\n\u003cp\u003eThreading in python is used to run multiple threads (tasks, function calls) at the same time.\u003c/p\u003e\n\n![Threading](docs/threading.png)\n\n```python\nimport threading\nimport time\nimport datetime as dt \n\ndef task1():\n    for i in range(0, 100):\n        time_now = dt.datetime.utcnow()\n        color = \"\\033[92m\"\n        default_color = \"\\033[00m\"\n        print(f'{time_now} :{color} Working on the First Task {default_color}')\n        time.sleep(1)\n\ndef task2():\n    for i in range(0, 100):\n        time_now = dt.datetime.utcnow()\n        color = \"\\033[94m\"\n        default_color = \"\\033[00m\"\n        print(f'{time_now} :{color} Working on the Second Task {default_color}')\n        time.sleep(1)\n\n# step1: define target(function)\nthread1 = threading.Thread(target=task1)\nthread2 = threading.Thread(target=task2)\n\n# step2: start thread\nthread1.start()\nthread2.start()\n```\n\n# **PyChat** cmd implementation with tcp in python\n\n\u003cp\u003esending messages between server and client\u003c/p\u003e\n\n![PyChat-CMD](docs/PyChat-CMD.png)\n\n### Server side implementation\n```python\nimport socket\nimport threading\n\nserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nserver.bind(('localhost', 12345))\nserver.listen(16)\nconn, addr = server.accept()\n\ndef send_data():\n    while True:\n        message = str.encode(\"server: \" + input())\n        conn.send(message)\n\ndef recv_data():\n    while True:\n        message_recv = conn.recv(1024)\n        print(\"\\033[96m {}\\033[00m\" .format(message_recv.decode()))\n\nthreading.Thread(target=send_data).start()\nthreading.Thread(target=recv_data).start()\n```\n\n### Client side implementation\n```python\nimport socket\nimport threading\n\nclient = socket.socket()\nclient.connect(('localhost', 12345))\n\ndef send_data():\n    while True:\n        message = str.encode(\"client: \" + input())\n        client.send(message)\n\ndef recv_data():\n    while True:\n        message_recv = client.recv(1024)\n        print(\"\\033[92m {}\\033[00m\" .format(message_recv.decode()))\n\nthreading.Thread(target=send_data).start()\nthreading.Thread(target=recv_data).start()\n```\n\n# **PyChat** GUI implementation with tcp in python\n\n\u003cp\u003econnect server and client to same ip\u003c/p\u003e\n\n![input ip panel](docs/input-ip-panel.png)\n\n\u003cp\u003esending messages between server and client\u003c/p\u003e\n\n![sending-message-panel.png](docs/sending-message-panel.png)\n\n\n### Server side implementation\n```python \nimport socket\nimport threading\nimport time\nfrom tkinter import *\n\ndef set_ip():\n    ip = edit_text_ip.get()\n    port = edit_text_port.get()\n    \n    # Define Server:\n    server = socket.socket()\n    server.bind((ip, int(port)))\n    server.listen()\n\n    global conn\n    conn, addr = server.accept()\n\n    # distryo input root\n    input_root.destroy()\n    # end of input root\n    input_root.quit()\n\n\ndef send():\n    if str(edit_text.get()).strip() != \"\":\n        message = str.encode(edit_text.get())\n        conn.send(message)\n\n        # scrollbar:\n        listbox.insert(END, message)\n        edit_text.delete(0, END)\n\n        # raw labels:\n        # label = Label(root, text=message, bg=\"#00802b\", fg=\"white\")\n        # label.pack(fill=X, side=TOP)\n\n    # after sent message\n    edit_text.delete(0, END)\n\n\ndef recv():\n    while True:\n        recv_message = conn.recv(1024).decode()\n\n        # scrollbar:\n        listbox.insert(END, \"Client: \" + recv_message)\n        edit_text.delete(0, END)\n\n        # raw labels:\n        # label = Label(root, text = \"Client : \" + recv_message, bg=\"#2929a3\", fg=\"white\")\n        # label.pack(fill=X, side=TOP)\n\n\n# Server GUI:\n\n# 1: Input Root GUI\ninput_root = Tk()\n\nedit_text_ip = Entry()\nedit_text_port = Entry()\nip_label = Label(input_root, text=\"Enter IP:\")\nport_label = Label(input_root, text=\"Enter Port:\")\nwatermark = Label(input_root, text=\"Developed by: M-Taghizadeh\", fg=\"#66000f\")\nconnect_btn = Button(input_root, text=\"Connect To Server\", command=set_ip, bg='#668cff', fg=\"white\")\n\n# show elements:\nip_label.pack(fill=X, side=TOP)\nedit_text_ip.pack(fill=X, side=TOP)\nport_label.pack(fill=X, side=TOP)\nedit_text_port.pack(fill=X, side=TOP)\nwatermark.pack(fill=X, side=BOTTOM)\nconnect_btn.pack(fill=X, side=BOTTOM)\n\ninput_root.title(\"Server\")\ninput_root.geometry(\"300x300\")\ninput_root.resizable(width=False, height=False)\n\ninput_root.mainloop()\n\n# 2: Main Root GUI\nroot = Tk()\n\n# Scrollbar:\nscrollbar = Scrollbar(root)\nscrollbar.pack(side=RIGHT, fill=Y)\nlistbox = Listbox(root, yscrollcommand=scrollbar.set)\nlistbox.pack(fill=BOTH, side=TOP)\nscrollbar.config(command=listbox.yview)\n\nbutton = Button(root, text=\"Send Message\", command=send, bg='#29a329', fg=\"white\")\nedit_text = Entry(root)\n\nbutton.pack(fill=X, side=BOTTOM)\nedit_text.pack(fill=X, side=BOTTOM)\n\nroot.title(\"PyChat [Server]\")\nroot.geometry(\"310x260\")\nroot.resizable(width=False, height=False)\n\nthreading.Thread(target=recv).start()\n\nroot.mainloop()\n```\n\n### Client side implementation\n```python\nimport socket\nimport threading\nimport time\nfrom tkinter import *\n\ndef set_ip():\n    ip = edit_text_ip.get()\n    port = edit_text_port.get()\n\n    # Define Client and connect to server:\n    global client\n    client = socket.socket()\n    client.connect((ip, int(port)))\n\n    # distryo input root\n    input_root.destroy()\n    # end of input root:\n    input_root.quit()\n\n\ndef send():\n    if str(edit_text.get()).strip() != \"\":\n        message = edit_text.get()\n        client.send(str.encode(message))\n\n        # scrollbar:\n        listbox.insert(END, message)\n        edit_text.delete(0, END)\n\n        # raw labels:\n        # label = Label(canvas, text=message, bg=\"#00802b\", fg=\"white\")\n        # label.pack(fill=X, side=TOP)\n\n\ndef recv():\n    while True:\n        response_message = client.recv(1024).decode()\n\n        # scrollbar:\n        listbox.insert(END, \"Server: \" + response_message)\n        edit_text.delete(0, END)\n\n        # raw labels:\n        # label = Label(root, text = \"Server : \" + response_message, bg=\"#2929a3\", fg=\"white\")\n        # label.pack(fill=X, side=TOP)\n\n\n# Client GUI\n# 1: Input Root GUI\ninput_root = Tk()\n\nedit_text_ip = Entry()\nedit_text_port = Entry()\nip_label = Label(input_root, text=\"Enter Server IP\")\nport_label = Label(input_root, text=\"Enter Server Port\")\nwatermark = Label(input_root, text=\"Developed by: M-Taghizadeh\", fg=\"#66000f\")\nconnect_btn = Button(input_root, text=\"Connect To Server\", command=set_ip, bg='#668cff', fg=\"white\")\n\n# show elements:\nip_label.pack(fill=X, side=TOP)\nedit_text_ip.pack(fill=X, side=TOP)\nport_label.pack(fill=X, side=TOP)\nedit_text_port.pack(fill=X, side=TOP)\nwatermark.pack(fill=X, side=BOTTOM)\nconnect_btn.pack(fill=X, side=BOTTOM)\n\ninput_root.title(\"Client\")\ninput_root.geometry(\"300x300\")\ninput_root.resizable(width=False, height=False)\n\ninput_root.mainloop()\n\n# 2: Main Root GUI\nroot = Tk()\n\n# Scrollbar:\nscrollbar = Scrollbar(root)\nscrollbar.pack(side=RIGHT, fill=Y)\nlistbox = Listbox(root, yscrollcommand=scrollbar.set)\nlistbox.pack(fill=BOTH, side=TOP)\nscrollbar.config(command=listbox.yview)\n\nbutton = Button(root, text=\"Send Message\", command=send, bg='#4040bf', fg=\"white\")\nbutton.pack(fill=X, side=BOTTOM)\nedit_text = Entry(root)\nedit_text.pack(fill=X, side=BOTTOM)\n\nroot.title(\"PyChat [Client]\")\nroot.geometry(\"310x260\")\nroot.resizable(width=False, height=False)\n\nthreading.Thread(target=recv).start()\n\nroot.mainloop()\n```\n\n# Dependencies\n- socket\n- threading\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-taghizadeh%2Fpychat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fm-taghizadeh%2Fpychat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-taghizadeh%2Fpychat/lists"}