用于将 1 字节字符发送到 IP 地址的按钮

发布于 2024-12-20 21:08:39 字数 265 浏览 2 评论 0原文

我是Python新手,我正在寻找一种将1字节字符(例如:字母“D”)发送到IP地址的方法。这是用来控制机器人的,所以我只需要前进、后退、向左和向右。我在网上做了一些研究,它建议使用套接字连接到 IP 地址,但这对我来说似乎有点令人困惑。我已经在我的网页中制作了4个按钮,但我不太确定如何让网页在用户单击按钮时向IP地址发送信号(例如:如果用户按“右”按钮,网页将发送一个单字节字符“r”到IP地址)

任何帮助将不胜感激

ps我使用的网络方法之间有什么大的区别吗?就像wifi和3G之间

I am new to python and I am looking for a way to send a 1 byte character (for example: the letter "D") to an ip address. This is used to control a robot so all I need are forward, backward, left and right. I've done some research online and it suggests using sockets to connect to the ip address but it seems kind of confusing to me. I have made 4 buttons in my webpage already but I am not too sure on how to make the webpage send out the signal to the ip address when the user click on the button (for example: if the user press the "right" button, the webpage will send a one byte character "r" to the ip address)

Any help would be greatly appreciated

ps would there be any big difference between the networking method i use? like between wifi and 3G

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在梵高的星空下 2024-12-27 21:08:39

套接字很简单,尤其是在 Python 中! :)

这是一个简单的程序,用于向某个 IP 地址发送一封信:

import socket

# Each address on the Internet is identified by an ip-address
# and a port number.
robot_ip_address = "192.168.0.12"  # Change to applicable
robot_port       = 3000            # Change to applicable

# Create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to somewhere...
s.connect((robot_ip_address, robot_port))

# Send one character to the socket
s.send('D')

# Close the socket after use
s.close()

机器人当然需要一个类似的程序来接收命令:

import socket

robot_port = 3000  # Change to applicable

# Create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# "Bind" it to all ip-addresses on the local host, and a specific port
s.bind(("", robot_port))

# Tell the socket to listen for connections
s.listen(5)

while True:
    # Wait for a new connection
    print "Waiting for connection..."
    (c, c_addr) = s.accept()

    print "New connection from: ", c_addr

    while True:
        try:
            command = c.recv(1)
        except socket.error, e:
            print "Error: %r" % e
            break;

        if command == 'D':
            # Do something for the 'D' command
            print "Received command 'D'"
        elif command == '':
            print "Connection closed"
            break
        else:
            print "Unknown command, closing connection"
            break

    c.close()

正如您所看到的,需要编写和理解的代码非常少。您实际上不必了解网络和 TCP/IP 的大部分工作原理,只需了解套接字用于通过 Internet 进行通信即可。 :)

复制第一个程序,每个按钮一个,并修改发送到服务器的内容。然后你有四个程序发送不同的命令,连接到你的按钮。

了解有关 Python 套接字的更多信息此处此处

Sockets are easy, especially in Python! :)

This is a simple program to send a single letter to some ip-address:

import socket

# Each address on the Internet is identified by an ip-address
# and a port number.
robot_ip_address = "192.168.0.12"  # Change to applicable
robot_port       = 3000            # Change to applicable

# Create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to somewhere...
s.connect((robot_ip_address, robot_port))

# Send one character to the socket
s.send('D')

# Close the socket after use
s.close()

The robot of course needs a similar program to receive commands:

import socket

robot_port = 3000  # Change to applicable

# Create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# "Bind" it to all ip-addresses on the local host, and a specific port
s.bind(("", robot_port))

# Tell the socket to listen for connections
s.listen(5)

while True:
    # Wait for a new connection
    print "Waiting for connection..."
    (c, c_addr) = s.accept()

    print "New connection from: ", c_addr

    while True:
        try:
            command = c.recv(1)
        except socket.error, e:
            print "Error: %r" % e
            break;

        if command == 'D':
            # Do something for the 'D' command
            print "Received command 'D'"
        elif command == '':
            print "Connection closed"
            break
        else:
            print "Unknown command, closing connection"
            break

    c.close()

As you can see, there is very little code to write and understand. You don'r really have to understand most of how networking and TCP/IP works, just that a socket is used to communicate over the Internet. :)

Duplicate the first program, one for each button, and modify what is sent to the server. Then you have four programs sending different commands, to be connected to your buttons.

Read more on Python sockets here, and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文