Skip to content

Instantly share code, notes, and snippets.

@1ycx
Last active November 30, 2020 19:29
Show Gist options
  • Save 1ycx/f65a11ace7975990965343d0a92f17b1 to your computer and use it in GitHub Desktop.
Save 1ycx/f65a11ace7975990965343d0a92f17b1 to your computer and use it in GitHub Desktop.

Revisions

  1. 1ycx revised this gist Feb 10, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ try:
    print("Data : ", message.decode())
    print("Address : ", address)

    s.sendto(data.encode(), ("<broadcast>", server_port))
    s.sendto(data.encode(), ('<broadcast>', server_port))

    except (KeyboardInterrupt, SystemExit):
    exit()
    @@ -45,7 +45,7 @@ s.bind((host, port))
    data = "Secret key : s76fshg23"

    try:
    s.sendto(data.encode(), ("<broadcast>", server_port))
    s.sendto(data.encode(), ('<broadcast>', server_port))

    message, address = s.recvfrom(8192)
    print("Got data, addr from server - ")
    @@ -68,7 +68,7 @@ except (KeyboardInterrupt, SystemExit):
    import socket
    import time
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(("localhost", 0))
    sock.bind(('', 0))
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)

    while True:
  2. 1ycx revised this gist Feb 10, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    Run server and client in seperate terminals. Server first then client.

    ## `Server.py`
    ### `Server.py`
    ```python3
    import socket

    @@ -29,7 +29,7 @@ except (KeyboardInterrupt, SystemExit):
    exit()
    ```

    ## `Client.py`
    ### `Client.py`
    ```python3
    import socket

  3. 1ycx revised this gist Feb 10, 2019. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -68,16 +68,17 @@ except (KeyboardInterrupt, SystemExit):
    import socket
    import time
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(('',0))
    sock.bind(("localhost", 0))
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)

    while True:
    m = '{0}\n'.format(time.time())
    sock.sendto(m,("<broadcast>",5566))
    data = '{0}\n'.format(time.strftime("%H:%M:%S", time.localtime()))
    sock.sendto(data.encode(), ('<broadcast>', 5566))
    time.sleep(5)
    ```
    *Client* :
    ```bash
    $ nc -k -w 1 -ul 5566
    22:45:58
    22:45:58
    22:45:45
    22:45:50
    ```
  4. 1ycx revised this gist Feb 10, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -72,7 +72,7 @@ sock.bind(('',0))
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
    while True:
    m = '{0}\n'.format(time.time())
    sock.sendto(m,('<broadcast>',5566))
    sock.sendto(m,("<broadcast>",5566))
    time.sleep(5)
    ```
    *Client* :
  5. 1ycx revised this gist Feb 10, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Docs
    # Docs :

    Run server and client in seperate terminals. Server first then client.

    @@ -58,7 +58,7 @@ except (KeyboardInterrupt, SystemExit):
    ```


    **Notes**
    ## Notes :

    - Check out this interesting pythonsheet on **`Socket`** : https://www.pythonsheets.com/notes/python-socket.html
    - Another way to broadcast (run in two terminals) :
  6. 1ycx revised this gist Feb 10, 2019. 3 changed files with 58 additions and 48 deletions.
    59 changes: 58 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,62 @@

    Run server and client in seperate terminals. Server first then client.

    ## `Server.py`
    ```python3
    import socket

    host = ''
    port = 51424
    server_port = 51423

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    s.bind((host, port))

    data = "Secret key : s76fshg23"

    try:
    message, address = s.recvfrom(8192)
    print("Got data, addr from client - ")
    print("Data : ", message.decode())
    print("Address : ", address)

    s.sendto(data.encode(), ("<broadcast>", server_port))

    except (KeyboardInterrupt, SystemExit):
    exit()
    ```

    ## `Client.py`
    ```python3
    import socket

    host = ''
    port = 51424
    server_port = 51423

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    s.bind((host, port))

    data = "Secret key : s76fshg23"

    try:
    s.sendto(data.encode(), ("<broadcast>", server_port))

    message, address = s.recvfrom(8192)
    print("Got data, addr from server - ")
    print("Data : ", message.decode())
    print("Address : ", address)

    except (KeyboardInterrupt, SystemExit):
    exit()

    ```


    **Notes**

    - Check out this interesting pythonsheet on **`Socket`** : https://www.pythonsheets.com/notes/python-socket.html
    @@ -22,5 +78,6 @@ while True:
    *Client* :
    ```bash
    $ nc -k -w 1 -ul 5566
    1431473025.72
    22:45:58
    22:45:58
    ```
    24 changes: 0 additions & 24 deletions client.py
    Original file line number Diff line number Diff line change
    @@ -1,24 +0,0 @@
    import socket

    host = "127.0.0.1"
    port = 51424
    server_port = 51423

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    s.bind((host, port))

    data = "Secret key : s76fshg23"

    try:
    s.sendto(data.encode(), (host, server_port))

    message, address = s.recvfrom(8192)
    print("Got data, addr from server - ")
    print("Data : ", message.decode())
    print("Address : ", address)
    # Acknowledge it.

    except (KeyboardInterrupt, SystemExit):
    exit()
    23 changes: 0 additions & 23 deletions server.py
    Original file line number Diff line number Diff line change
    @@ -1,23 +0,0 @@
    import socket

    host = "127.0.0.1"
    port = 51423
    client_port = 51424

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    s.bind((host, port))

    data = "Secret key : 6sc45hj90"

    try:
    message, address = s.recvfrom(8192)
    print("Got data, addr from client - ")
    print("Data : ", message.decode())
    print("Address : ", address)
    # Acknowledge it.

    s.sendto(data.encode(), (host, client_port))
    except (KeyboardInterrupt, SystemExit):
    exit()
  7. 1ycx revised this gist Feb 10, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@ Run server and client in seperate terminals. Server first then client.

    - Check out this interesting pythonsheet on **`Socket`** : https://www.pythonsheets.com/notes/python-socket.html
    - Another way to broadcast (run in two terminals) :

    *Server* :
    ```python3
    import socket
  8. 1ycx revised this gist Feb 10, 2019. 1 changed file with 21 additions and 1 deletion.
    22 changes: 21 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,24 @@

    Run server and client in seperate terminals. Server first then client.

    Notes - Check out this interesting pythonsheet on **`Socket`** : https://www.pythonsheets.com/notes/python-socket.html
    **Notes**

    - Check out this interesting pythonsheet on **`Socket`** : https://www.pythonsheets.com/notes/python-socket.html
    - Another way to broadcast (run in two terminals) :
    *Server* :
    ```python3
    import socket
    import time
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(('',0))
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
    while True:
    m = '{0}\n'.format(time.time())
    sock.sendto(m,('<broadcast>',5566))
    time.sleep(5)
    ```
    *Client* :
    ```bash
    $ nc -k -w 1 -ul 5566
    1431473025.72
    ```
  9. 1ycx revised this gist Feb 10, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,4 @@

    Run server and client in seperate terminals. Server first then client.

    Notes - Check out this intresting pythonsheet on **`Socket`** : https://www.pythonsheets.com/notes/python-socket.html
    Notes - Check out this interesting pythonsheet on **`Socket`** : https://www.pythonsheets.com/notes/python-socket.html
  10. 1ycx revised this gist Feb 10, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Docs

    Run server and client in seperate terminals. Server first then client.
    Run server and client in seperate terminals. Server first then client.

    Notes - Check out this intresting pythonsheet on **`Socket`** : https://www.pythonsheets.com/notes/python-socket.html
  11. 1ycx revised this gist Feb 10, 2019. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions client.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import socket

    host = ''
    host = "127.0.0.1"
    port = 51424
    server_port = 51423

    @@ -12,7 +12,7 @@
    data = "Secret key : s76fshg23"

    try:
    s.sendto(data.encode(), ('<broadcast>', server_port))
    s.sendto(data.encode(), (host, server_port))

    message, address = s.recvfrom(8192)
    print("Got data, addr from server - ")
    4 changes: 2 additions & 2 deletions server.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import socket

    host = ''
    host = "127.0.0.1"
    port = 51423
    client_port = 51424

    @@ -18,6 +18,6 @@
    print("Address : ", address)
    # Acknowledge it.

    s.sendto(data.encode(), ('<broadcast>', client_port))
    s.sendto(data.encode(), (host, client_port))
    except (KeyboardInterrupt, SystemExit):
    exit()
  12. 1ycx created this gist Feb 10, 2019.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # Docs

    Run server and client in seperate terminals. Server first then client.
    24 changes: 24 additions & 0 deletions client.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import socket

    host = ''
    port = 51424
    server_port = 51423

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    s.bind((host, port))

    data = "Secret key : s76fshg23"

    try:
    s.sendto(data.encode(), ('<broadcast>', server_port))

    message, address = s.recvfrom(8192)
    print("Got data, addr from server - ")
    print("Data : ", message.decode())
    print("Address : ", address)
    # Acknowledge it.

    except (KeyboardInterrupt, SystemExit):
    exit()
    23 changes: 23 additions & 0 deletions server.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import socket

    host = ''
    port = 51423
    client_port = 51424

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    s.bind((host, port))

    data = "Secret key : 6sc45hj90"

    try:
    message, address = s.recvfrom(8192)
    print("Got data, addr from client - ")
    print("Data : ", message.decode())
    print("Address : ", address)
    # Acknowledge it.

    s.sendto(data.encode(), ('<broadcast>', client_port))
    except (KeyboardInterrupt, SystemExit):
    exit()