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:
- Programming udp sockets in python
- python 3.6 socket tcp Connect TypeError: a bytes-like object is required, not ‘str’
- Python Error: Socket TypeError: a bytes-like object is required, not ‘str’ [How to Solve]
- Python Error: TypeError: string argument without an encoding
- Python TypeError: can only concatenate str (not “int”) to str
- Using Python HDFS module to operate Hadoop HDFS
- Python calculator error: can’t multiply sequence by non-int of type ‘float’
- Code a packet sniffer in python with pcapy exte…
- [Solved] psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?
- [Solved] appium Error: Message: Cannot set the element to ‘XXXX’. Did you interact with the correct element?