Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Network Programming
#11
Did you try it?

Is that your ip, or the ip of a different computer? You can't bind to a port of an ip that isn't your computer, that's not how networks work.
Reply
#12
(May-11-2017, 05:13 PM)nilamo Wrote: Did you try it?

Is that your ip, or the ip of a different computer?  You can't bind to a port of an ip that isn't your computer, that's not how networks work.
I'm about to try it..

Thats actually the IP of the board that is connected to my system!
I'm echoing data from that board through that port (7)
I need to know whether this program will help me read the data from that port!

(Mar-31-2017, 11:56 AM)wavic Wrote:
import asyncio
import socket

host = '0.0.0.0'
port = 8888

loop = asyncio.get_event_loop()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setblocking(False)
s.bind((host, port))
s.listen(10)

async def handler(conn):
    while True:
        data = await loop.sock_recv(conn, 65535)
        if not data:
            break
        await loop.sock_sendall(conn, data)
    conn.close()

async def server():
    while True:
        conn, addr = await loop.sock_accept(s)
        loop.create_task(handler(conn))

loop.create_task(server())
loop.run_forever()
loop.close()
The code is not mine. I've found it in my Python_tutorials folder. I was digging for async/await a lot and still don't know how to use it.
The code is not working! Lots of syntax errors.. or that's what the compiler suggests!
Reply
#13
Show us the errors. 
I gave you an example or hint how you could do it. Is hard to fit your need if you almost copy/paste it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#14
Hmm, if you use TCP port 7 (Echo protocol) you will only receive what you transmit and is usually used for round trip time testing.  Is that what you want?  If you want to actually communicate with another computer, you will want to use a port greater than 8000 and actually greater than 10000 is even better, in order to avoid TCP/UDP standard ports and to a lesser extent port scanning attacks.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020