练习了下socket测试端口,目前采用了ulipad编写,其中打印中文时遇到错误:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)。

 

上网查询资料,解决方法

s=u"中文"

print s.encode('gb2312')
 
 
#coding=utf-8
import socket
import time
import thread

socket.setdefaulttimeout(9)

def socketTest(port,ip):
    try:
        if int(port) > 65536:
            print u'端口不正确,需小于65535'.encode('gb2312')
        else:
            s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            result = s.connect_ex(ip,port)
            if result == 0:
                print ip,':',port,u'端口开放'.encode('gb2312')
            s.close()
    except:
        print u'扫描端口异常!'.encode('gb2312')
if __name__ == '__main__':
    socketTest('338889','127.0.0.1')

  

相关文章:

  • 2021-11-19
  • 2022-12-23
  • 2021-11-19
  • 2021-11-19
  • 2021-11-20
  • 2021-09-22
猜你喜欢
  • 2022-01-18
  • 2021-11-29
  • 2022-03-08
  • 2021-11-19
  • 2021-10-22
  • 2022-12-23
相关资源
相似解决方案