Why is my stdout interfering with my webpage in py

2019-07-23 04:03发布

Really simple code that just fires one pcap (packet) using scapy,

If I just want to do simple cgi-bin POSTS to myself to run a set of 10 easy tests why is this just kicking back as text (rather than a website). If I comment out the line

sendp(a, iface="em1")

Then the below code actually generates the website fine... but it won't actually send the packet, I imagine this is something with stdout.... suggestions are welcome!

#!/usr/local/bin/python

from scapy.all import *
#import v6tester_main

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>NPD Automation Tool - GCT-USG</title>'
print '</head>'
print '<body>'
print '<font> NPD Automation Tool </font>'

a = Ether() / IP() / IPv6() / ICMPv6EchoRequest()
sendp(a, iface="em1")

print '<br>'
print '<font>End of Test</font>'
print '</body>'
print '</html>'

If I view source I see this->

<html>
<head>
<title>NPD Automation Tool - GCT-USG</title>
</head>
<body>
<font> NPD Automation Tool </font>

Sent 1 packets.
<br>
<font>End of Test</font>
</body>
</html>

3条回答
成全新的幸福
2楼-- · 2019-07-23 04:22

Most probably the output of sendp brakes the HTML so bad it shows as text, what you can do is try pass verbose=0 to sendp (if the output is not important), or try other verbose level. if the output of sendp is important to you, you can run it in a separate script with subprocess.Popen and try to format the output so it fit in the HTML page.

edit: ops, someone already answered with almost the same

查看更多
Animai°情兽
3楼-- · 2019-07-23 04:35

It doesn't appear that you are sending http data(eg Response Headers).

You should be because it is being run off a web server.

查看更多
Animai°情兽
4楼-- · 2019-07-23 04:47

As I understand it, sendp doesn't just echo the package to stdout; it sends it "down the wire" at a lower protocol level. So if you want to send an html header you'll need to wrap it inside a package, not the other way around.

But are you sure you need to mess with scapy? If all you want is to send POST requests to a webserver, you can just use urllib2.urlopen. Put your POST data in the optional data argument, and it will use POST instead of GET for the request.

查看更多
登录 后发表回答