python - 在我的脚本中获取 "TypeError: an integer is required"

标签 python string sockets python-2.7 irc

出于教育原因,我正在尝试使用 Python 创建僵尸网络,但我不断收到以下错误:

TypeError: an integer is required

这是我的脚本:

import os
import socket
import random
import string

# string.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
a = random.choice(string.letters) 
b = random.choice(string.letters)
c = random.choice(string.letters)
d = random.choice(string.letters)   
e = random.choice(string.letters)
name = 'bot' + a + b + c + d + e

network = 'chat.freenode.net'
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
print irc.recv ( 4096 )

irc.send ('NICK', name + '\r\n')
irc.send ( 'USER', name, name, name, ':Python IRC\r\n' )
irc.send ( 'JOIN #occult_hand\r\n' )
irc.send ( 'PRIVMSG #occult_hand :Hello World.\r\n' )

while True:
    data = irc.recv ( 4096 )
    if data.find ( 'PING' ) != -1:
        irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
    if data.find ( '!shutdown' ) != -1:
        irc.send ( "PRIVMSG #occult_hand :Fine, if you don't want me\r\n" )
        irc.send ( 'QUIT\r\n' )
    if data.find ( '!list' ) != -1:
        irc.send ( 'PRIVMSG #occult_hand :' + name, 'ONLINE\r\n' )
    if data.find ( '!ddos' ) != -1:
        irc.send ( 'PRIVMSG #occult_hand :Enter a target\r\n' )
    if data.find ( 'KICK' ) != -1:
        irc.send ( 'JOIN #occult_hand\r\n' )
    if data.find ('cheese') != -1:
        irc.send ( 'PRIVMSG #occult_hand :WHERE!!!!!!\r\n' )
    print data

最佳答案

第 21 行的脚本错误,irc.send('NICK', name + '\r\n'):

Traceback (most recent call last):
  File "botnet.py", line 21, in 
    irc.send('NICK', name + '\r\n')
TypeError: an integer is required

It's because the socket.send method has the following signature, as per the docs:

socket.send(string[, flags])

string 参数是要发送的数据。 flags 参数是可选的标志,与 Unix man 2 recv 中描述的相同。参见 http://man7.org/linux/man-pages/man2/recv.2.html了解详情。

基本上,flags 参数是一个整数值,默认为 0。如 Unix 手册页所述,标志值在 socket 模块中作为常量提供,您可以通过使用 OR 逻辑运算组合所需的标志值来获得该值,例如:

socket.send(data, flags=socket.MSG_OOB | socket.MSG_DONTROUTE)

要修复您的脚本,您必须将所有要发送的数据连接到一个字符串中,并将其作为第一个参数传递给 socket.send 方法的所有位置:

irc.send('NICK' + name + '\r\n')
irc.send('USER' + name + name + name + ':Python IRC\r\n')
# ...

关于python - 在我的脚本中获取 "TypeError: an integer is required",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23586917/

相关文章:

python - 在 Ubuntu 14.04 (Beaglebone Black) 上使用带有 ROS 的 python Adafruit_BBIO GPIO 时出错

python - spacy 标记化合并了错误的标记

Python添加到一个列表是一个键的值

php - 使用 PHP 过滤掉重复 CSS 规则的最快方法是什么?

string - Scala 字符串按字符数量递减分割

sockets - 如何在不需要root权限的情况下运行类似traceroute的程序?

bcopy() 之后客户端的核心转储

c++ - 仅启用来自指定 IP 地址的传入连接

python - NGINX + Flask,没有 Gunicorn?

c - 如何访问字符串中特定范围的位