![]() |
|
Could someone help me with this project? - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Python (https://sinister.li/Forum-Python) +--- Thread: Could someone help me with this project? (/Thread-Could-someone-help-me-with-this-project) |
Could someone help me with this project? - ghostheadx9 - 09-16-2018 Here is my code: Code: import sys, socket
try:
rfc_number = int(sys.argv[1])
except (IndexError, ValueError):
print('Must supply an RFC number as first argument')
sys.exit(2)
host = 'www.ietf.org'
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',
'\r\n'
)
req = req.format(
rfcnum=rfc_number,
host=host,
port=port,
version=sys.version.info[0]
)
sock.sendall(req.encode('ascii'))
rfc_raw = bytearray()
while True:
buf = sock.recv(4096)
if not len(buf):
break
rfc_raw += buf
rfc = rfc_raw.decode('utf-8')
print(rfc)Could someone help me: 1. figure out what's causing the syntax error: Code: adam@adam-Inspiron-5558:~/.PyCharmCE2018.2/config/scratches$ python firstTCP_program.py 782
Traceback (most recent call last):
File "firstTCP_program.py", line 21, in <module>
req = req.format(
AttributeError: 'tuple' object has no attribute 'format'2. understand my own code better? I really want to understand the code better so that I can reprogram it differently. Thanks so much. Best, ghostheadx9 RE: Could someone help me with this project? - griimnak - 03-10-2019 Code: 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',
'\r\n'
)
req = req.format(
rfcnum=rfc_number,
host=host,
port=port,
version=sys.version.info[0]
).format() only works on single strings, req is a tuple because you used "," in between strings. This should work for you: Code: """
This is just dummy data for my test, delete this.
"""
rfcnum = "/blahblah"
host = "test"
port = 80
version = "testtest"
""" Take this req object and plug in your data here"""
req= '''
GET/rfc/rfc{}.txt HTTP/1.1
Host: {}:{}
User-Agent: Python {}
Connection: close
'''.format(rfcnum, host, port, version)
"""Just for my test, you can remove this."""
print(req)Code: C:\Users\griimnak\Desktop>py -2 test.py
GET/rfc/rfc/blahblah.txt HTTP/1.1
Host: test:80
User-Agent: Python testtest
Connection: close
C:\Users\griimnak\Desktop> |