插座很容易,特別是在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的工作原理,只需要使用套接字通過互聯網進行通信。 :)
複製第一個程序,每個按鈕一個,並修改發送到服務器的內容。然後你有四個程序發送不同的命令,連接到你的按鈕。
閱讀更多關於Python套接字here和here。
忘了提及我將使用tcp/ip作爲客戶端 – user1086652
可能的重複[使用Python注入原始TCP數據包](http://stackoverflow.com/questions/2912123/injecting-raw-tcp-packets-with -python) –
目前還不清楚這與Python有什麼關係。你在使用Python Web框架嗎?如果是這樣,請說明這是哪個框架 - 它可能是相關的。 –