if data.find('!search') != -1:
nick = data.split('!')[ 0 ].replace(':','')
conn = httplib.HTTPConnection("www.md5.rednoize.com")
conn.request("GET", "?q=" + arg)
response = conn.getresponse()
data = response.read()
result = re.findall('<div id="result" >(.+?)</div', data)
if result:
sck.send('PRIVMSG ' + chan + result + '\r\n')
else:
sck.send('PRIVMSG ' + chan + " :" ' could not find the hash' + '\r\n')
When I run this code I get this error:
conn.request("GET " + "?q=" + arg)
TypeError: cannot concatenate 'str' and 'list' objects
How can I fix this?
Where does arg come from? Do you know what it's supposed to contain?
arg is apparently a list, not a string. Try replacing arg with str(arg[0]) and see if that works.
Related
Very new to coding. This was the initial code I was trying to get to run but have found it is incompatible with python 3 because of how it handles strings and bytes.
def opensocket():
s = socket.socket()
s.connect((HOST,PORT))
s.send("PASS " + PASS + "\r\n")
s.send("NICK " + IDENT + "\r\n")
s.send("JOIN #" + CHANNEL + "\r\n")
return s
I think I need to encode these strings into bytes but all the conversion methods I've tried have given errors. here are some i've tried.
def opensocket():
s = socket.socket()
s.connect((HOST,PORT))
s.send(b"PASS " + PASS + "\r\n")
s.send(b"NICK " + IDENT + "\r\n")
s.send(b"JOIN #" + CHANNEL + "\r\n")
and
def opensocket():
s = socket.socket()
s.connect((HOST,PORT))
s.send("PASS " + PASS + "\r\n".encode())
s.send("NICK " + IDENT + "\r\n".encode())
s.send("JOIN #" + CHANNEL + "\r\n".encode())
return s
both of these give errors saying the str can't be concatenated into bytes
any help?
edit: went through the suggested post but I'm gaining nothing from that. it looks so different and I'm so new to this that I can't even tell where the string was he was trying to convert
In the case that PASS, IDENT and CHANNEL are bytes:
def opensocket():
s = socket.socket()
s.connect((HOST,PORT))
s.send(b"PASS " + PASS + b"\r\n")
s.send(b"NICK " + IDENT + b"\r\n")
s.send(b"JOIN #" + CHANNEL + b"\r\n")
In the case that PASS, IDENT and CHANNEL are str:
def opensocket():
s = socket.socket()
s.connect((HOST,PORT))
s.send(b"PASS " + PASS.encode('ascii') + b"\r\n")
s.send(b"NICK " + IDENT.encode('ascii') + b"\r\n")
s.send(b"JOIN #" + CHANNEL.encode('ascii') + b"\r\n")
It's important to know what protocol you're using, to know what encoding actually to use. 'ascii' is a safe bet, but it means you're limited in to ASCII-only characters, so if the protocol actually specifies UTF-8 strings, you should do .encode('utf-8') instead.
I have 3 lat/lngs and a URL that I am constructing. My output should be 3 URLs, for each lat/lng, I am receiving 6. What do I need to change in my code below to print 3 URLs instead of 6? The try block and first for loops start are error handling, if the script fails, try twice. I am getting 6 values even when the script does not fail.
def main():
for i in range(2):
for attempts in range (1):
try:
for lat, lon, id_, startDate, endDate in zip(latval, lonval, idVal, startDayValStr, endDayValStr):
time_param = '?start='+ startDate +'T'+ "01:00" + 'Z' + '&end='+ endDate + 'T' + "23:00" + 'Z'
hrPrecip = 'https://insight.api.wdtinc.com/hourly-precipitation/' + str(lat)+'/' + str(lon) + time_param + '&unit=inches'
print hrPrecip
except Exception as e:
attempts = i + 1
sleep(30)
print "now trying attempt #" + " " + str(attempts) + " " + "for error" " " + str(e)
print(traceback.format_exc())
logging.exception(e)
msg = "PYTHON ERRORS:\nTraceback info:\n" + traceback.format_exc()
logging.debug("sending error email")
emailserver.email_error_msg(msg)
if __name__ == "__main__":
main()
Output:
https://insight.api.wdtinc.com/hourly-precipitation/44.797207/-95.175648?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.796302/-95.180946?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.778728/-95.23022?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.797207/-95.175648?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.796302/-95.180946?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.778728/-95.23022?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches`
It could be the try: and except: block. failing in the first. I am guessing you do not need the second loop with attempt in range(1). In fact you do not need any loop here.
import socket, sys, string
if len(sys.argv) !=4:
print ("UsageL ./ninjabot.py <server> <port> <channel>")
sys.exit(1)
irc = sys.argv[1]
port = int(sys.argv[2])
chan = sys.argv[3]
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.connect((irc, port))
sck.send(b'NICK ninjabot\r\n')
sck.send(b'USER ninjabot ninjabot ninjabot :ninjabot Script\r\n')
sck.send('JOIN ' + " " + chan + '\r\n')
data = ''
while True:
data = sck.recv(1024)
if data.find('PING') != -1:
sck.send('PONG ' + data.split() [1] + '\r\n')
print (data)
print (data)
I get this error in sck.send('JOIN ' + " " + chan + '\r\n') but when i try this:
sck.send(b'JOIN ' + " " + chan + '\r\n')
I get TypeError: can't concat str to bytes
I know there's similar posts with this same issue, but none of those seem to help me.
This is due to the fact that b'JOIN' is of type bytes while " " as well as chan and "\r\n" are of type str.
2 corrections:
chan = sys.argv[3].encode()
sck.send(b'JOIN ' + b" " + chan + b'\r\n')
alternatively you could just:
sck.send(b'JOIN %s\r\n' % chan.encode())
this would be the best style. Similar corrections will be need to be made later in your snippet as well. socket.send is looking for a bytes string, so ensure that all strings which you pass it are either b"" or encoded otherwise using encode()
So a few hours ago I decided to try my hands on sockets with python and build a simple irc bot. So far I'm having some trouble getting it connected to the server. I get the following erros:
b":irc.ku.cx 439 * :Please wait while we process your connection.\r\n:irc.ku.cx NOTICE AUTH :*** Couldn't look up your hostname (cached)\r\n"
b':irc.ku.cx NOTICE AUTH :*** Checking Ident\r\n'
b':irc.ku.cx NOTICE AUTH :*** No Ident response\r\n'
After that it stalls out. But about a minute of it running I suddenly get an endless amount of b"", each in a new line (probably something to do with the while loop in the code). This is my code:
import socket
server = 'irc.rizon.net'
channel = '#linux'
nick = 'MyBot'
port = 6667
ircsock = socket.socket()
ircsock.connect((server, port))
ircsock.send(bytes('"NICK " + nick', 'UTF-8'))
ircsock.send(bytes('"USER " + nick + " " + nick + " " + nick + " :" + nick', 'UTF-8'))
while True:
data = ircsock.recv(2048)
print (data)
if data.find(b"PING") != -1:
ircsock.send(b"PONG :" + data.split(':')[1])
Thanks for any help you guys can provide.
As icktoofay said, there are extra quotes in your code. Also, in IRC you need to add a line break (\r\n) to the end of every command.
Replace these lines:
ircsock.send(bytes('"NICK " + nick', 'UTF-8'))
ircsock.send(bytes('"USER " + nick + " " + nick + " " + nick + " :" + nick', 'UTF-8'))
with
ircsock.send(bytes("NICK " + nick + "\r\n", 'UTF-8'))
ircsock.send(bytes("USER " + nick + " " + nick + " " + nick + " :" + nick + "\r\n", 'UTF-8'))
and it should work.
By the way, I recommend using socket.makefile() instead, which handles buffering and encoding for you. Here's your code modified to use the file interface:
import socket
server = 'irc.rizon.net'
channel = '#linux'
nick = 'MyBot'
port = 6667
ircsock = socket.socket()
ircsock.connect((server, port))
handle = ircsock.makefile(mode='rw', buffering=1, encoding='utf-8', newline='\r\n')
print('NICK', nick, file=handle)
print('USER', nick, nick, nick, ':'+nick, file=handle)
for line in handle:
line = line.strip()
print(line)
if "PING" in line:
print("PONG :" + line.split(':')[1], file=handle)
Here, I use the print function which inserts spaces and newlines automatically.
if data.find('!google') != -1:
nick = data.split('!')[ 0 ].replace(':','')
try:
gs = GoogleSearch(args)
gs.results_per_page = 1
results = gs.get_results()
for res in results:
sck.send('PRIVMSG ' + chan + " " + res.title.encode("utf8") + '\r\n')
sck.send('PRIVMSG ' + chan + " " + res.url.encode("utf8") + '\r\n')
print
except SearchError, e:
sck.send('PRIVMSG ' + chan + " " + "Search failed: %s" % e + " " + '\r\n')
Ok I'm trying to make the script wait a few seconds before another user can "!google" to prevent users from flooding the channel or the bot, not sure if I should use the sleep() function because that might stop the whole script, I just want to make it wait a few seconds before anyone can use "!google" again.
There is a sleep function inside the time module.
However, to make your script not block, you can call the time function in the time module and store that. If the current time is less than that plus, say, five seconds, don't allow them to use it.
For example:
last_google = 0
# somewhere later in the script where last_google is still in scope...
if data.find('!google') != -1:
if last_google + 5 < time.time():
# throttled
return
last_google = time.time()
# do something here