Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:使用套接字时需要整数_Python_Sockets_Integer_Typeerror_Required - Fatal编程技术网

Python TypeError:使用套接字时需要整数

Python TypeError:使用套接字时需要整数,python,sockets,integer,typeerror,required,Python,Sockets,Integer,Typeerror,Required,研究: 以下是我的完整错误输出: Traceback (most recent call last): File "/home/promitheas/Desktop/virus/socket1/socket1.py", line 20, in <module> createSocket() File "/home/promitheas/Desktop/virus/socket1/socket1.py", line 15, in createSocket ServerSoc

研究:

以下是我的完整错误输出:

Traceback (most recent call last):
File "/home/promitheas/Desktop/virus/socket1/socket1.py", line 20, in <module>
createSocket()
File "/home/promitheas/Desktop/virus/socket1/socket1.py", line 15, in    createSocket
ServerSock.bind((socket.gethostname(), servPort))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
TypeError: an integer is required
回溯(最近一次呼叫最后一次):
文件“/home/promitheas/Desktop/virus/socket1/socket1.py”,第20行,在
createSocket()
createSocket中第15行的文件“/home/promitheas/Desktop/virus/socket1/socket1.py”
ServerSock.bind((socket.gethostname(),servPort))
文件“/usr/lib/python2.7/socket.py”,第224行,meth格式
返回getattr(self.\u sock,name)(*args)
TypeError:需要一个整数
代码:

导入套接字
#获取本地公共IP地址
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s、 连接(('8.8.8.8',0))
#定义一些变量
servIpAddr=s.getsockname()[0]
servPort=''
while((len(servPort)<4)):#和(len(servPort)>65535)
servPort=原始输入(“输入服务器端口。必须至少为4位数字。\n>”)
#创建套接字以等待连接
def createSocket():
ServerSock=socket.socket(socket.AF\u INET,socket.SOCK\u流)
ServerSock.bind((socket.gethostname(),servPort))#这就是发生错误的地方
ServerSock.listen(5)
(clientsocket,address)=ServerSock.accept()
如果名称=“\uuuuu main\uuuuuuuu”:
createSocket()

我不确定是否还有其他错误,但我真的被这个错误难住了。请询问您是否需要任何其他信息,并提前感谢

看起来地址元组的第二个元素需要是整数。从文件中:

一对(主机、端口)用于AF_INET address系列,其中主机是一个字符串,表示Internet域表示法中的主机名,如'darin.cwi.nl'或IPv4地址,如'100.50.200.5',端口是一个整数

bind
中使用之前,请尝试将
servPort
转换为整数

servPort = ''
while ((len(servPort) < 4)): # and (len(servPort) > 65535)
    servPort = raw_input("Enter server port. Must be at least 4     digits.\n> ")
servPort = int(servPort)
servPort=''
while((len(servPort)<4)):#和(len(servPort)>65535)
servPort=原始输入(“输入服务器端口。必须至少为4位数字。\n>”)
servPort=int(servPort)

servPort
必须是整数。您当前已将其设置为用户输入的字符串。尝试使用
int(servPort)
将原始输入强制转换为
int

servPort = ''
while ((len(servPort) < 4)): # and (len(servPort) > 65535)
    servPort = raw_input("Enter server port. Must be at least 4     digits.\n> ")
servPort = int(servPort)