Why is my stdout interfering with my webpage in python?

妖精的绣舞 提交于 2019-12-11 07:28:45

问题


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>

回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/9507602/why-is-my-stdout-interfering-with-my-webpage-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!