how to code TCP in python?
You didn't put commas after each variable:

Your code:

req = req.format(

Edited code:

import sys, socket

  rfc_number = int(sys.argv[1])
except (IndexError, ValueError):
  print('Must supply an RFC number as first argument')

host = ''
port = 80
sock = socket.create_connection((host, port))

req = (
  'GET/rfc/rfc{rfcnum}.txt HTTP/1.1\r\n'
  'Host: {host}:{port}\r\n'
  'User-Agent: Python {version}\r\n'
  'Connection: close\r\n'

req = req.format(

rfc_raw = bytearray()
while True:
  buf = sock.recv(4096)
  if not len(buf):
  rfc_raw += buf
rfc = rfc_raw.decode('utf-8')

Also you should never use sys for arguments, use argparse or optparse instead. You also have a lot of readability issues. A better and more pythonic way to write this would be something like this:

import sys
import socket
import argparse

class Parser(argparse.ArgumentParser):

   class to store your arguments in

   def __init__(self):
       super(Parser, self).__init__()

   def optparse():
       staticmethod so that python knows this is apart of the class but does not
       generate data from the class itself
       parser = argparse.ArgumentParser()
       parser.add_argument("-h", "--host", dest="hostToConnect", help="pass a host")
       parser.add_argument("-p", "--port", type=int, dest="portToConnect", help="pass a port")
       parser.add_argument("-r", "--rfc", type=int, dest="rfcNumber", help="pass an RFC number")
       return parser.parse_args()

# constants for what is going to be used throughout the entire program
SOCK = socket.create_connection

def create_packet(rfc_number, host, port, py_version=sys.version_info):
   create your packet in a function for readability
   packet = "GET /rfc/rfc{}.txt HTTP/1.1\r\n"
   packet += "Host: {}:{}\r\n"
   packet += "User-Agent: Python {}\r\n"
   packet += "Connection: close\r\n"
   packet += "\r\n"
   return packet.format(
       rfc_number, host, port, py_version

def create_request(rfc, host, port):
   create your request in a function and generate the packet from the function
   that way if you want to do multiple hosts at once, it is simple and easy
   req = create_packet(rfc, host, port)
   raw_rfc = bytearray()
   socks = SOCK((host, port))
   while True:
       recv_buffer = socks.recv(BUFFER_SIZE)
       if not len(recv_buffer):
       raw_rfc += recv_buffer
   rfc = raw_rfc.decode("utf-8")
   return rfc

def main():
   main function of the program where you call everything
   opts = Parser().optparse()
   if opts.hostToConnect is None:
       print("must specify a host")
   if opts.portToConnect is None:
       print("must specify a port")
   if opts.rfcNumber is None:
       print("must specify an RFC number")

   data = create_request(opts.rfcNumber, opts.hostToConnect, opts.portToConnect)

# this tell python the entry point of your program
if __name__ == "__main__":

As you can see the one I wrote reads more like python and can be simply and easily edited to make multiple connections if needed.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Ebook Collection [89 Files] Insider 15 88,552 08-12-2021, 08:02 PM
Last Post: zzeuss
  NSA Python Training Insider 4 29,014 08-12-2021, 02:14 AM
Last Post: hworth
  Having an issue writing a python script with vim FancyBear 4 22,694 01-03-2021, 11:27 PM
Last Post: FancyBear
  Python Data structures and algorithms resources skinnyj0shua 1 17,485 12-23-2020, 12:52 PM
Last Post: enmafia2