New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get server IP address for HTTP/1.1 Responses #3940
Merged
Merged
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
80925ab
Get server IP address for HTTP/1.1 responses
elacuesta e8da7e2
Test DNS resolution using CrawlerProcess
elacuesta 8529dff
Update docs regarding Response.ip_address and IPv6
elacuesta b9e3a62
Merge branch 'master' into response_ip_address
elacuesta a2ae380
Remove unnecessary commas
elacuesta bb8f7dc
Mock DNS server
elacuesta 4851efd
Flake8 adjustments
elacuesta e0ef8ad
CrawlerRunner test for Response.ip_address
elacuesta 13670f0
Ignore tests/CrawlerRunner directory
elacuesta ad70497
Remove unnecessary parentheses in class definition
elacuesta 6c33349
Merge branch 'master' into response_ip_address
elacuesta c2f484d
Merge branch 'master' into response_ip_address
Gallaecio 13ba9bc
Note about Response.ip_address
elacuesta 037ae5b
Explicitly indicate None as ip_address’s default value
Gallaecio a44942d
Merge branch 'master' into response_ip_address
elacuesta f85bf77
Restore unrelated change
elacuesta 889b471
Import changes
elacuesta 3aa5eab
Merge branch 'master' into response_ip_address
elacuesta ac73bcc
Merge branch 'master' into response_ip_address
elacuesta 91a78ee
Pass callback results as dicts instead of tuples
elacuesta 1785095
Remove single-use variable
elacuesta 1f2e2a6
Merge branch 'master' into response_ip_address
elacuesta c922992
Tests: Move code inside __main__ block
elacuesta File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from urllib.parse import urlparse | ||
|
||
from twisted.internet import reactor | ||
from twisted.names.client import createResolver | ||
|
||
from scrapy import Spider, Request | ||
from scrapy.crawler import CrawlerRunner | ||
from scrapy.utils.log import configure_logging | ||
|
||
from tests.mockserver import MockServer, MockDNSServer | ||
|
||
|
||
class LocalhostSpider(Spider): | ||
name = "localhost_spider" | ||
|
||
def start_requests(self): | ||
yield Request(self.url) | ||
|
||
def parse(self, response): | ||
netloc = urlparse(response.url).netloc | ||
self.logger.info("Host: %s" % netloc.split(":")[0]) | ||
self.logger.info("Type: %s" % type(response.ip_address)) | ||
self.logger.info("IP address: %s" % response.ip_address) | ||
|
||
|
||
with MockServer() as mock_http_server, MockDNSServer() as mock_dns_server: | ||
port = urlparse(mock_http_server.http_address).port | ||
url = "http://not.a.real.domain:{port}/echo".format(port=port) | ||
|
||
servers = [(mock_dns_server.host, mock_dns_server.port)] | ||
reactor.installResolver(createResolver(servers=servers)) | ||
|
||
configure_logging() | ||
runner = CrawlerRunner() | ||
d = runner.crawl(LocalhostSpider, url=url) | ||
d.addBoth(lambda _: reactor.stop()) | ||
reactor.run() |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we follow it well for other attributes, but it'd be good to say that it can be None as well. Maybe also mention when this may happen ("not all download handlers may support this attribute" or something like that, maybe in a more user-friendly way, as nobody knows what's a download handler).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a note about this. I intentionally didn't mention the S3 handler, which uses the HTTP handler internally, because those responses are technically also http. Also didn't mention responses with no body (https://github.com/scrapy/scrapy/pull/3940/files#diff-18150b1d259c93bf10bf1d4e5028d753R384-R386), I think that's probably a very specific edge case.