Created
December 8, 2013 09:09
-
-
Save Jxck/7854971 to your computer and use it in GitHub Desktop.
TCP Fast Open – Webを速くするためにGoogleがやっていること Make the Web Faster 4 -
Python サンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, socket | |
''' | |
tcp fast open server sample code | |
execute it before enable tfo flag | |
$ echo 3 | sudo tee /proc/sys/net/ipv4/tcp_fastopen | |
''' | |
PORT = 80 | |
MSG_FASTOPEN = 0x20000000 | |
def client(): | |
# tfo cookie request | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.sendto("hello\n", MSG_FASTOPEN, (sys.argv[1], PORT)) | |
print s.recv(6) | |
s.close() | |
# tfo data | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.sendto("hello\n", MSG_FASTOPEN, (sys.argv[1], PORT)) | |
print s.recv(6) | |
s.close() | |
def main(): | |
client() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
''' | |
tcp fast open server sample code | |
execute it before enable tfo flag | |
$ echo 3 | sudo tee /proc/sys/net/ipv4/tcp_fastopen | |
''' | |
PORT = 80 | |
TCP_FASTOPEN = 23 | |
def server(): | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_TCP, TCP_FASTOPEN, 5) | |
s.bind(("", PORT)) | |
s.listen(1024) | |
while True: | |
conn, addr = s.accept() | |
msg = conn.recv(6) | |
print msg | |
conn.send(msg) | |
conn.close() | |
s.close() | |
def main(): | |
server() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment