1- #!/usr/bin/env python2
1+ #!/usr/bin/env python3
22# coding:utf-8
33import socket
44import serial
55import time
66import logging
77import logging .handlers
88import re
9+ import fcntl
10+ import struct
911import RPi .GPIO as GPIO
1012
1113
@@ -21,7 +23,7 @@ def get_logger(name):
2123 # console.setFormatter(formatter)
2224
2325 save = logging .handlers .RotatingFileHandler (
24- "/home/ubuntu/mycobot_server .log" , maxBytes = 10485760 , backupCount = 1 )
26+ "server .log" , maxBytes = 10485760 , backupCount = 1 )
2527 save .setFormatter (formatter )
2628
2729 logger .addHandler (save )
@@ -40,19 +42,19 @@ def __init__(self, host, port):
4042 self .mc = None
4143 self .s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
4244 self .s .bind ((host , port ))
43- print "Binding succeeded!"
45+ print ( "Binding succeeded!" )
4446 self .s .listen (1 )
4547 self .connect ()
4648
4749 def connect (self ):
4850 while True :
4951 try :
50- print "waiting connect!------------------"
52+ print ( "waiting connect!------------------" )
5153 conn , addr = self .s .accept ()
5254 port_baud = []
5355 while True :
5456 try :
55- print "waiting data--------"
57+ print ( "waiting data--------" )
5658 data = conn .recv (1024 )
5759 command = data .decode ('utf-8' )
5860 if data .decode ('utf-8' ) == "" :
@@ -120,6 +122,9 @@ def re_data_2(self, command):
120122
121123
122124if __name__ == "__main__" :
123- HOST = socket .gethostbyname (socket .gethostname ())
125+ ifname = "wlan0"
126+ s = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
127+ HOST = socket .inet_ntoa (fcntl .ioctl (s .fileno (), 0x8915 , struct .pack ('256s' , bytes (ifname ,encoding = "utf8" )))[20 :24 ])
124128 PORT = 9000
129+ print ("ip: {} port: {}" .format (HOST , PORT ))
125130 MycobotServer (HOST , PORT )
0 commit comments