python TypeError: an integer is required

Problem Description:

When using socket locally to transfer data to NetAssist, it is found that Python error typeerror: an integer is required error is reported after executing Python file

code:

  1 #!/usr/bin/env python3
  2 from socket import *
  3 udpSocket = socket(AF_INET, SOCK_DGRAM)
  4 destIp = input('enter ip:')
  5 destPort = input('enter port:')
  6 destData = input('enter data:')
  7 
  8 udpSocket.sendto(destData.encode('gb2312'),(destIp, destPort))

execution result:

enter ip:192.168.162.1
enter port:8080
enter data:haha
Traceback (most recent call last):
  File "udp-code.py", line 8, in <module>
    udpSocket.sendto(destData.encode('gb2312'),(destIp, destPort))
TypeError: an integer is required (got type str)

cause:

After querying the python document, it is found that the parameters passed do not meet the requirements of the socket. SendTo () method

Document Description:

s.sendto (string [, flag], address)
send UDP data. Send data to the socket. Address is a tuple in the form of (IPADDR, port) and specifies the remote address. The return value is the number of bytes sent
destport should be of type int

modify code:

  1 #!/usr/bin/env python3
  2 from socket import *
  3 udpSocket = socket(AF_INET, SOCK_DGRAM)
  4 destIp = input('enter ip:')
  5 destPort = int(input('enter port:'))
  6 destData = input('enter data:')
  7 
  8 udpSocket.sendto(destData.encode('gb2312'),(destIp, destPort))

result:

Summary:

After an error is reported, analyze the cause of the error according to the information

Query relevant documents and whether the parameter format meets the requirements

Similar Posts: