Skip to content

Commit 614b8b9

Browse files
author
marcusw
committed
Improved server_prompt example, got rid of the "~" junk (that was a bad idea in the first place...), and put a success message in for find_brick.
1 parent c49bfe3 commit 614b8b9

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

examples/server_prompt.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
import socket
1+
#This script is an example for the nxt.server module. You need to run
2+
#nxt.server.serve_forever() in another window. Or, if you want to use
3+
#this across a network, pass the IP of the computer running the server
4+
#as an argument in the command line.
5+
6+
import socket, sys
7+
8+
try:
9+
server = sys.argv[1]
10+
bindto = ''
11+
except:
12+
server = 'localhost'
13+
bindto = 'localhost'
214

315
insock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
416
outsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
5-
insock.bind(('localhost', 54374))
17+
insock.bind((bindto, 54374))
618

719
while 1:
820
command = raw_input('nxt> ')
9-
outsock.sendto(command, ('localhost', 54174))
21+
outsock.sendto(command, (server, 54174))
1022
retvals, addr = insock.recvfrom(1024)
1123
retcode = retvals[0]
12-
retmsg = retvals[1:retvals.index('~')]
24+
retmsg = retvals[1:len(retvals)]
1325
print 'Return code: '+retcode
1426
print 'Return message: '+retmsg

nxt/server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1111
# GNU General Public License for more details.
1212

13-
'''Use for a socket interface NXT driver.'''
13+
'''Use for a socket-interface NXT driver. Command and protocol docs at:
14+
http://code.google.com/p/nxt-python/wiki/ServerUsage.'''
1415

1516
import nxt.locator
1617
from nxt.motor import *
@@ -56,6 +57,7 @@ def _process_command(cmd):
5657
try:
5758
brick = nxt.locator.find_one_brick()
5859
brick = brick.connect()
60+
retmsg = 'Connected to brick.'
5961
retcode = 0
6062
except:
6163
retcode = 1
@@ -172,7 +174,7 @@ def serve_forever():
172174
code, message = _process_command(inmsg)
173175

174176
#send return code to the computer that send the request
175-
outsock.sendto(str(code) + message + '~', (addr[0], 54374))
177+
outsock.sendto(str(code) + message, (addr[0], 54374))
176178

177179
#print a summany of the response
178180
print 'Send return code '+str(code)+' with message "'+message+'" to '+addr[0]

0 commit comments

Comments
 (0)