5

I have a TCP server for which I need to write a client in Python.

The server is an arduino using the arduino_uip library; the server code is (almost) the same as the TCP server example of that library. It works fine using nc as a client.

But when I use python sockets (as in this answer) to communicate with the server, the server hangs on socket shutdown or close.

That may be an issue with the server; however since nc is working fine as a client, my question is :

What does this answer do differently from nc that may explain server hanging on connection shutdown/close)?

Summing up what works & what not :

  • python client & nc -l as server: works
  • nc as client & arduino server : works
  • python client & arduino server: hangs server

Here is the client code :

import socket

def netcat(hostname, port, content):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((hostname, port))
    s.sendall(content)
    s.shutdown(socket.SHUT_WR)
    while 1:
        data = s.recv(1024)
        if data == "":
            break
        print "Received:", repr(data)
    print "Connection closed."
    s.close()

Edit :

It appears (Vorsprung's answer made me think about it!)that it is in fact a timing problem. If I add sleep(0.5) before shutdown in the above code all works nicely (as in netcat where there is a manual delay before I hit Ctrl+C). I suppose I'll have to check that arduino library now...

4

2 回答 2

2

查看了 netcat 源代码( svn checkout svn://svn.code.sf.net/p/netcat/code/trunk netcat-code ),它只在关闭之前调用 shutdown() ,而不仅仅是在设置之后插座

据我所知,这就是区别

于 2013-11-06T19:16:48.227 回答
0

相当于netcat的python

使用 nclib:pip install nclib

文档: https ://nclib.readthedocs.io/en/latest/

Python 3 netcat 监听器,输入和输出分别分配给变量命令和数据。

import nclib


def listener(port):
    """ local netcat listener for reverse bash shell from a remote host. """
    server = nclib.TCPServer(('0.0.0.0', int(port)))
    print("listening ...")
    for client in server:
        print('Connected to %s:%d' % client.peer)
        command = ""
        while command != "exit":
            try:
                # if command was entered by the user
                if len(command) > 0:
                    # read the line to hide command from output
                    if command in client.readln().decode('utf-8').strip(" "):
                        pass  # disregard the last command

                # get output until dollar sign (bash --posix forces bash-X.X$)
                data = client.read_until('$')
                print(data.decode('utf-8'), end="")  # print string of received bytes

                # get user input command and write command to socket
                command = input(" ")
                client.writeln(command)

            # handle exceptions and exiting
            except KeyboardInterrupt:
                print("\nKeyboardInterrupt")
                exit(1)
            except Exception as e:
                print("\nException Occurred\n")
                print(e)
                exit(1)
        print("Disconnected :-)")
        exit(1)
于 2021-07-24T18:54:45.280 回答