2

I'm using macOS and have netcat installed via Homebrew

$ brew info netcat
netcat: stable 0.7.1 (bottled)
Utility for managing network connections
http://netcat.sourceforge.net/
/usr/local/Cellar/netcat/0.7.1 (11 files, 104.2K) *
  Poured from bottle on 2016-06-21 at 12:19:18
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/netcat.rb

I'm just trying to get netcat to respond to an incoming curl request, but I think the problem I'm having is that netcat doesn't know when to respond.

Here is the content expected to be sent back, it's stored in response.txt:

HTTP/1.1 200 OK

Here is the netcat command being run in one terminal shell:

sudo nc -l -p 80 < response.txt 

Here is the curl command being run in another terminal shell:

curl 127.0.0.1:80

Could someone help me understand what I would need to do in order to get this working as intended.

I can get this to work by using netcat instead of curl for the connection, but ideally I'd like a non-netcat client (such as curl or a web browser) to make a connection to localhost:80 and have the first instance of netcat respond.

Thanks.

4
  • 2
    In nc you cannot use the -p flag together with the -l flag. That does not make sense and will throw an error. This is clearly explained in the man pages. Commented Oct 22, 2016 at 16:11
  • 1
    Using this instead works without issue for me: sudo nc -l 80 < response.txt. The curl command receives and outputs the text stored in the file response.txt Commented Oct 22, 2016 at 16:13
  • @arkascha thanks for the reply, I see now you're quite correct with regards to the use of -p not making any sense, but trying sudo nc -l 80 < response.txt in one shell and curl -v 127.0.0.1:80 in another doesn't work for me. I get curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused Commented Oct 25, 2016 at 10:29
  • The same works without issue for me, I just tried again just to be certain. There must be some other issue on your side. Maybe a local firewall? Commented Oct 25, 2016 at 10:34

2 Answers 2

2

Try using the macOS netcat instead of homebrew, and drop the -p option:

sudo /usr/bin/nc -l 80 < response.txt
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure what the difference is between gnu's version and macOS' though (with regards to this particular example)? But hey, it worked. Thanks :-)
Note that nc will send the response as soon as the connection is established, which won't always be before the client sends the HTTP GET request. For many clients like Chrome and Firefox this doesn't seem to be an issue, but for at least Safari this is treated as an error and no request gets made
-1

Keep in mind to include the full request details (Response, Headers, etc..) in "response.txt"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.