Scapy: How to manipulate Host in http header?
<p>I wrote this piece of code to get http header and set Host:</p>

<pre><code>http_layer = packet.getlayer(http.HTTPRequest).fields
http_layer['Host'] = "newHostName"
return packet
</code></pre>

<p>After running the afforementioned code,the new host name has been set correctly, but the problem is that when I write the packet in pcap file, I still see the previous host in http fields,
Is there an absolute way to manipulate <code>http_layer['Host']</code> ?
Any help would be appreciated.
Regards.</p>

<p>After all, found the answer.
The key is that <code>scapy</code> firstly parses <code>HTTP Request</code> and shows the dict of its fields. So when we try to assign a new field like <code>Host</code>, it changes the <code>Host</code> which it has already parsed and does not change the original field value.
So, this is the way to modify <code>Host</code> or any other respective fields:</p>

<pre><code>str_headers = pkt['HTTP']['HTTP Request'].fields['Headers']
str_headers = str_headers.replace('Host: ' + pkt['HTTP']['HTTP Request'].fields['Host'], 'Host: ' + new_val)
pkt['HTTP']['HTTP Request'].fields['Headers'] = str_headers
return pkt
</code></pre>

python packet scapy