python socket超时

[root@yyjk ~]# time python a1.py
Mon May  7 09:32:31 2018
a
1
<type 'str'>
a
<type 'str'>
1
Traceback (most recent call last):
  File "a1.py", line 46, in <module>
    a.Sms('a',18072722237)
  File "a1.py", line 27, in Sms
    s.connect((host,port))
  File "/usr/local/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.timeout: timed out

real	0m40.076s
user	0m0.031s
sys	0m0.007s
[root@yyjk ~]# vim a1.py
[root@yyjk ~]# cat a1.py
# -*- coding: utf-8 -*-
import SocketServer
import socket
import time
import sys
reload(sys)
print time.ctime()
sys.setdefaultencoding('utf-8')
class SMSsendx (object):
  def Sms(self,a,b):
     message=a
     phone=b
     #.decode('utf-8').encode('gbk')
     print a
     print len(message)
     print type(message)
     message=message.encode('gbk')
     print message
     print type(message)
     print len(message)
     host='10.3.229.91'
     port=745
     bufsize=1024
     addr=(host,port)
     s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     s.settimeout(40)
     s.connect((host,port))
     data=""
     tab=chr(9)
     #my $data=^Xd$tab;
     data=chr(28)+chr(48)+chr(88)+chr(100)+tab
     data +='015802'+tab
     data +=tab
     data +='N'+tab
     data +=message+tab
     print data
     data +=str(phone)+tab
     #msg = data.encode('gbk')
     s.send(data)
     data = s.recv(bufsize)
        # print data.strip()
     cur_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
     print ("%s receice from server :%s") % (cur_time,data)
     s.close()
a=SMSsendx()
a.Sms('a',18072722237)
print time.ctime()

原文地址:https://www.cnblogs.com/hzcya1995/p/13349222.html